使用 Bluebird 承诺对 gcloud 进行承诺
Promisify gcloud using Bluebird promise
我想对 gcloud 包中的所有方法进行 promisify,但在执行时出现错误。
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 包吗?
根据有关 Promisification 的文档:
Promisifies the entire object by going through the object's properties
and creating an async equivalent of each function on the object and
its prototype chain.
所以,然后你承诺 gcloud
对象,它与承诺桶无关,因为它是另一个 class。
您可以为每个 Bucket 实例执行此操作
Promise.promisifyAll(bucket);
或者对Bucket的prototype进行一次补丁应用
Promise.promisifyAll(require('gcloud/lib/storage/bucket').prototype);
我想对 gcloud 包中的所有方法进行 promisify,但在执行时出现错误。
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 包吗?
根据有关 Promisification 的文档:
Promisifies the entire object by going through the object's properties and creating an async equivalent of each function on the object and its prototype chain.
所以,然后你承诺 gcloud
对象,它与承诺桶无关,因为它是另一个 class。
您可以为每个 Bucket 实例执行此操作
Promise.promisifyAll(bucket);
或者对Bucket的prototype进行一次补丁应用
Promise.promisifyAll(require('gcloud/lib/storage/bucket').prototype);