简体   繁体   English

结合使用bluebirdjs和Google Cloud Datastore节点客户端库

[英]Using bluebirdjs with Google Cloud Datastore node client library

I am having a strange issue trying to use bluebirds promises with google's cloud client libraries. 尝试在Google的云客户端库中使用Bluebirds Promise,我遇到了一个奇怪的问题。 After digging around Google's libs, I noticed in the common functions, there is a util file with promisify functions. 在研究了Google的库之后,我注意到在常用功能中,有一个带有promisify函数的util文件。 I assume these are being used to promisify the datastore acess functions, as they can be used as a promise. 我假设这些被用来使数据存储访问功能多样化,因为它们可以用作承诺。 My problem comes in because I am writing a Google Cloud Function which executes based on a PubSub trigger. 出现我的问题是因为我正在编写一个基于PubSub触发器执行的Google Cloud Function。 At the end of the function I need to execute the callback() function to 'end' the function. 在函数的结尾,我需要执行callback()函数以“结束”函数。

I wanted to use bluebirds finally() api on the promises to make sure callback is always called. 我想在promises上使用bluebirds finally() api以确保始终调用回调。 But, when trying to access the datastore it returns it's own type of Promise and not a bluebird promise, even if I try call: 但是,当尝试访问数据存储区时,即使我尝试调用,它也会返回它自己的Promise类型,而不是蓝鸟承诺。

 const Promise = require('bluebird'); const Datastore = Promise.promisifyAll(require('@google-cloud/datastore')); const datastore = Promise.promisifyAll( Datastore({ projectId: 'xxxx' })); 

But this doesn't seem to 'replace' Google's promises with bluebirds. 但这似乎并不能用蓝鸟“代替” Google的承诺。 Is there a way to do this? 有没有办法做到这一点?

My Current work around: 我当前的工作:

 dothing(value) .then(function(){ return doAnotherThing(anothervalue); }) .then(function(){ // Done callback(); }) .catch(function(){ // Something went wrong callback(); }); 

I know this isn't that much more than a finally() but it still feels a little inelegant 我知道这不是比一个更加finally()但它仍然感觉有点不雅

Welp, that was a little embarrassing. Welp,这有点尴尬。 So, instead of taking full responsibility for making such an obvious mistake, I will blame the bluebird documentation. 因此,我不会对犯这样一个明显的错误承担全部责任,而是将责任归咎于bluebird文档。 (It's not, it's entirely my fault for not reading properly) (不是,这是我不正确阅读的完全错误)

Anyway, in order to use bluebird with datastore, I just needed to append Async to the function call. 无论如何,为了将bluebird与数据存储区结合使用,我只需要在函数调用中附加Async

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

Super simple 超级简单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM