【发布时间】:2016-07-27 20:40:14
【问题描述】:
我想承诺 gcloud 包中的所有方法,但我在这样做时出错了。
const Promise = require('bluebird');
const gcloud = Promise.promisifyAll(require('gcloud'));
//no problem when passing a callback into it
bucket.getFiles((err, files) => {
console.log(files)
})
//Complain error -> bucket.getFilesAsync is not a function
bucket.getFilesAsync().then((files) => {
console.log(files)
})
当我在promisifyAll 之后调用async 方法时,它会抱怨bucket.getFilesAsync is not a function,所以我尝试使用 ES6 承诺来承诺该方法。
const bucketFiles = new Promise((resolve, reject) => {
bucket.getFiles((err, files) => {
if (err) return reject(err);
resolve(files);
});
});
bucketFiles.then((files) => {
console.log(files)
})
这种使用 ES6 方式的承诺是有效的,但我不想通过这样做来承诺每种方法。所以,我想让bluebird 为我承诺一切。
我可以知道如何使用 bluebird 承诺 gcloud 包吗?
【问题讨论】:
-
这是什么
bucket? -
可能你需要
Promise.promisifyAll(bucket); -
可能是
Promise.promisifyAll(require('gcloud/lib/storage/bucket').prototype)? -
当你打电话给
Promise.promisifyAll(bucket);时它正在工作,我可以知道为什么这是工作但不是Promise.promisifyAll(require('gcloud'))
标签: node.js google-app-engine promise bluebird es6-promise