Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Functions for Firebase: serializing Promises

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...

like image 793
jazzgil Avatar asked Mar 18 '17 14:03

jazzgil


1 Answers

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.

like image 69
Frank van Puffelen Avatar answered Oct 02 '22 20:10

Frank van Puffelen