【问题标题】:Using bluebirdjs with Google Cloud Datastore node client library将 bluebirdjs 与 Google Cloud Datastore 节点客户端库一起使用
【发布时间】: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


    【解决方案1】:

    Welp,这有点尴尬。因此,我不会为犯下如此明显的错误承担全部责任,而是将责任归咎于 bluebird 文档。 (不是,完全是我看书不好)

    无论如何,为了在数据存储中使用 bluebird,我只需要将 Async 附加到函数调用中。

    function doTheThing(keys){
      return datastore.getAsync(keys);
    }

    超级简单

    【讨论】:

      猜你喜欢
      • 2016-10-12
      • 1970-01-01
      • 2017-04-13
      • 2014-04-23
      • 1970-01-01
      • 2015-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多