Learn JavaScript Promises (Pt 3) for sequential and parallel work in Cloud Functions - Firecasts
Why Stack Overflow recommends it
Stack Overflow users link it when answering Firebase Cloud Functions questions about returning promises, chaining async calls and using Promise.all().
Recommended by 8 people across 8 different months, 2018 to 2021.
What it is
Part 3 of Doug Stevenson's Firecasts series on JavaScript Promises, aimed at Cloud Functions for Firebase. It shows how to chain asynchronous work sequentially with promises and how to run work in parallel with Promise.all().
What people said
Strong rec Renaud Tarnec · 2018-04 · Stack Overflow · on “Firebase Document for each user?”
@NeeronBhatta Also, I would suggest that you have a look at these three videos from the Firebase team: [LINK] & [LINK] & [this video] A must for anyone starting coding for Cloud Functions. Good luck :-) !
Strong rec gvon79 · 2018-05 · Stack Overflow · on “Update value using the direct path instead of "an object"”
Doug Stevenson... that name seemed familiar... yep I've watched all your Firebase videos on youtube [this video] Firebase is awesome! And thank you for your tutorials!
Recommends Renaud Tarnec · 2018-04 · Stack Overflow · on “Firebase trigger functions works alternately”
The reason is that you have to chain the Promises (and therefore return a promise at each chaining step). The following should work: export const updateTotalAnswersCounts = functions.database .ref('/CurrentGame/answers/{aid}').onWrite((change, context) => { const collectionRef = change.after.ref.parent; let total = 0; return collectionRef.once('value', (snapshot) => { console.log('snapshot', snapshot.val()) snapshot.forEach((childSnapshot) => { if (childSnapshot.val() != null) { console.log('childSnapshot', childSnapshot.val()) total += Number(childSnapshot.val()) } }) }).then(() => { const updates = {} updates['/totalAnswersCount'] = total return firebase.ref('/CurrentGame').update(updates)
Recommends Renaud Tarnec · 2018-04 · Stack Overflow · on “Firebase Cloud function event - sometime not getting data on update event”
You don't have to call once within the Function since it is already returning the data at the location you are listening to, just listen to the parent node. So you should do like: exports.onNotificationUpdate = functions.database.ref('/Notification/{userId}/{notificationId}').onUpdate(event => { const notification = event.data.val(); if (notification === null) { console.error("Notification not found on notification update"); return null; //actually this would only be called in case of deletion of the Notification } else { //do something with the notification data: send Android notification, send mail, write in another node of the database, etc. //BUT return a Promise //notification const dec
Recommends Renaud Tarnec · 2018-05 · Stack Overflow · on “How to clone a node to another path based on a reference value from the initial path on Google Cloud Functions?”
The following should work. const admin = require("firebase-admin"); .... .... exports.updateRoom = functions.database.ref('/doors/{MACaddress}').onWrite((change, context) => { const afterData = change.after.val(); // data after the write const roomPushKey = afterData.inRoom; const ins = afterData.ins; const updates = {}; updates['/rooms/' + roomPushKey] = ins; return admin.database().ref().update(updates); }).catch(error => { console.log(error); //+ other rerror treatment if necessary }); Here are some explanations: You get the roomPushKey by reading the "data after the write" as an object: roomPushKey = afterData.inRoom . You don't need to do roomPushKey.once('child_added').then() Once you
Recommends Renaud Tarnec · 2018-05 · Stack Overflow · on “Add same child object in different parents firebase”
Ok. Then Cloud Functions is indeed the way to go. If you are starting with them I would suggest that you have a look at the three following videos from the Firebase team: [LINK] & [LINK] & [this video] And in addition there are some samples from Firebase the team here: [LINK]
Recommends Renaud Tarnec · 2018-05 · Stack Overflow · on “Adding or deleting data in a file with Cloud Functions”
There are several places in your code which need to be modified, in particular in terms of how to deal with promises. I would suggest you watch these three videos from the Firebase team: [LINK] and [LINK] and [this video] You will notice that in your case you don't need to use promise.all I think. Also have look at the Cloud Functions samples, a great way to study how code is organised: [LINK] I hope this helps.
Recommends Doug Stevenson · 2018-08 · Stack Overflow · on “Variable stays undefined after assigning within Promise”
get() (as well as all functions that return a promise) are asynchronous , meaning they return immediately. The callback provided to then() gets executed some time later when the async work is complete. Your code looks like it's assuming that get() blocks execution of this code, which it does not. The subsequent code that accesses dish_data is going to execute immediately after get() returns, which means it will be undefined. It won't get defined until your callback is finally executed. If you want to use the results of the get() , you should only do that in the callback, or pass it along to some other function from there. If you want to use async , then you will have to also use await to eff
Recommends Jbbae · 2018-08 · Stack Overflow · on “Firebase Functions "Error: memory limit exceeded. Function invocation was interrupted."”
Thx @renaud-tarnec! Those videos are also super-helpful. Made your suggested fix (makes complete sense!), but I'm also wondering if it's okay to put the **.then** after the **Promise.all**, instead of its parent promise (`return SkillPromise.then(...`) , as it shows up in the 3rd Firebase Promises video [this video] ? I'm still facing error-related issues, leading me to think that could be it...
Recommends naxxfish · 2018-10 · Stack Overflow · on “Apply an operation to each result in an Array resolved by a Promise without nesting”
This video entirely explains the solution to this, in the context of Firebase /Google Cloud Functions [this video]