最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cloud Functions for Firebase: serializing Promises - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question edited Mar 26, 2017 at 17:39 Doug Stevenson 318k36 gold badges455 silver badges473 bronze badges asked Mar 18, 2017 at 14:08 jazzgiljazzgil 2,3662 gold badges21 silver badges20 bronze badges 5
  • I'm curious why you're requesting /zzz/2 when you never use snaps[1]...? – T.J. Crowder Commented Mar 18, 2017 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 Commented Mar 18, 2017 at 14:17
  • Also note that the promise you're returning from your onWrite callback is resolved before the update at the end is plete. 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 Commented Mar 18, 2017 at 14:18
  • @T.J.Crowder I'd love to see an example of how its done. Thanks. – jazzgil Commented Mar 18, 2017 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 Commented Mar 18, 2017 at 14:21
Add a ment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论