stream-to-promise stream 2 promise 的npm 包

好处就是简化callback的处理,以下是关于minio listobjects 的使用

参考代码

  • pacakge.json
{
  "name": "app",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "minio": "^7.0.18",
    "stream-to-promise": "^3.0.0"
  }
}
  • app.js
const Minio = require('minio')
const streamToPromise = require('stream-to-promise') 
const minioClient = new Minio.Client({
    endPoint: 'play.min.io',
    port: 80,
    useSSL: false,
    accessKey: 'Q3AM3UQ867SPQQA43P2F',
    secretKey: 'zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG'
});
(async ()=>{
  var result = await  streamToPromise(minioClient.listObjectsV2("005-bucket","",true))
  console.log("exec 1")
  console.log(result)
})()
streamToPromise(minioClient.listObjectsV2("005-bucket","",true)).then(function(data){
    console.log("exec 2")
    console.log(data)
})

说明

通过stream-to-promise我们可以简化stream模式的数据访问

参考资料

https://github.com/bendrucker/stream-to-promise
https://docs.min.io/docs/javascript-client-quickstart-guide.html

posted on 2021-01-25 23:09  荣锋亮  阅读(156)  评论(0编辑  收藏  举报

导航