【发布时间】:2017-05-20 13:25:37
【问题描述】:
我在尝试将 bluebirds Promise 与 google 的云客户端库一起使用时遇到了一个奇怪的问题。
在挖掘了谷歌的库之后,我注意到在常用函数中,有一个带有 promisify 函数的 util 文件。
我假设这些被用来承诺数据存储访问功能,因为它们可以用作承诺。
我的问题出现了,因为我正在编写一个基于 PubSub 触发器执行的谷歌云函数。
在函数结束时,我需要执行 callback() 函数来“结束”函数。
我想在 Promise 上使用 bluebirds finally() api 以确保始终调用回调。但是,当尝试访问数据存储时,它返回它自己的 Promise 类型而不是 bluebird 承诺,即使我尝试调用:
const Promise = require('bluebird');
const Datastore = Promise.promisifyAll(require('@google-cloud/datastore'));
const datastore = Promise.promisifyAll(
Datastore({
projectId: 'xxxx'
}));
但这似乎并没有用蓝鸟“取代”谷歌的承诺。有没有办法做到这一点?
我目前的解决方法:
dothing(value)
.then(function(){
return doAnotherThing(anothervalue);
})
.then(function(){
// Done
callback();
})
.catch(function(){
// Something went wrong
callback();
});
我知道这那不仅仅是finally(),但它仍然感觉有点不雅
【问题讨论】:
标签: node.js google-cloud-datastore bluebird