In an onWrite handler, I'd like to perform multiple reads, manipulate some data, and then store it. I'm fairly new to the Promise concept. Am I safe with the following Promise handling, in regards to Firebase not killing my queries before they're done?
exports.test = functions.database.ref('/zzz/{uid}').onWrite(event => {
console.log('zzz', event.data.val());
return Promise.all([
admin.database().ref('/zzz/1').once('value'),
admin.database().ref('/zzz/2').once('value')
]).then(function(snaps) {
console.log('loaded', snaps[0].val());
var updKeys = {
["/xxx/" +event.params.uid +"/zoo"]: 'giraffe',
}
admin.database().ref().update(updKeys, function(error) {
console.log("Updating data finished. ", error || "Success.");
})
});
});
The above works, but not sure its the right way...
If your Function continues to execute after it has returned (or the promise that your function returns has resolved), Google Cloud Functions may interrupt your code at any time. There is however no guarantee that it will do so immediately.
In your code sample, you return the result of the final then()
. Since you don't return anything from within that then()
block, GCF may interrupt the call to update()
or it may continue to let the code run longer than is needed.
To correct this, make sure to "bubble up" the promise from the update()
call:
exports.test = functions.database.ref('/zzz/{uid}').onWrite(event => {
console.log('zzz', event.data.val());
return Promise.all([
admin.database().ref('/zzz/1').once('value'),
admin.database().ref('/zzz/2').once('value')
]).then(function(snaps) {
console.log('loaded', snaps[0].val());
var updKeys = {
["/xxx/" +event.params.uid +"/zoo"]: 'giraffe',
}
return admin.database().ref().update(updKeys, function(error) {
console.log("Updating data finished. ", error || "Success.");
})
});
});
In this code, the promise returned by update()
is the one being returned to GCF, which gives it the information to leave your function running for precisely as long as it needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With