I am having a strange issue trying to use bluebirds promises with google's cloud client libraries.
After digging around Google's libs, I noticed in the common functions, there is a util file with promisify functions.
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.
At the end of the function I need to execute the callback()
function to 'end' the function.
I wanted to use bluebirds finally()
api on the promises to make sure callback is always called. 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:
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. 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
Welp, that was a little embarrassing. So, instead of taking full responsibility for making such an obvious mistake, I will blame the bluebird documentation. (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.
function doTheThing(keys){
return datastore.getAsync(keys);
}
Super simple