10

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

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
jazzgil
  • 2,250
  • 2
  • 19
  • 20
  • I'm curious why you're requesting `/zzz/2` when you never use `snaps[1]`...? – T.J. Crowder Mar 18 '17 at 14:16
  • It's just a test - don't mind the actual (mis)usage of retrieved data (or the ugly fact its using hardcoded uids :) – jazzgil Mar 18 '17 at 14:17
  • Also note that the promise you're returning from your `onWrite` callback is resolved **before** the `update` at the end is complete. If that's not intentional, you'll need to create a promise, return it from `then`, and resolve it in the `update` callback. – T.J. Crowder Mar 18 '17 at 14:18
  • @T.J.Crowder I'd love to see an example of how its done. Thanks. – jazzgil Mar 18 '17 at 14:19
  • 1
    Well, I can't really answer the question you've asked (I don't know anything about when Firebase kills queries). If you want to ask a question about promises, I could answer that. Basically within the `then` you'd do `return new Promise((resolve, reject) => { /*...your current 'then' code here...*/});` and call `resolve` (or `reject`) from within the `update` callback. – T.J. Crowder Mar 18 '17 at 14:21

1 Answers1

10

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Ah! Missed a return. Thanks! :) Also, was wondering about which tag goes with firebase-functions, thanks. – jazzgil Mar 18 '17 at 14:30
  • Yeah, the naming is confusing. Sorry about that. Also see my explanation here: http://stackoverflow.com/questions/42854865/what-is-the-difference-between-cloud-function-and-firebase-functions/42859932#42859932 – Frank van Puffelen Mar 18 '17 at 15:13
  • I was using `event.data.adminRef.root.child('/x')`, but is it possible to use `admin.database().ref('/x')` here? – JCarlosR Aug 19 '17 at 02:33