javascript - Issue structuring a promise in a loop -


i struggling getting resolved, new promises.

i need first read both summative , formative firebase before can determine studentplacement

the way code below, provides null studentplacement snapshot.val(), not waiting x , y values.

exports.boxscoresupdate = functions.database.ref('/tests/{id}/testscores').onwrite(event => {      let testscr = 0;      (let = 1; <= section; i++) {         //         testscr += parseint(nvalue[i]);          var xindex = 0;         var yindex = 0;           admin.database().ref('testscores').child(data.key).child('summative').child(i).once("value").then(x => {              xindex = x.val();          });          admin.database().ref('testscores').child(data.key).child('formative').child(i).once("value").then(y => {               yindex = y.val();          });          admin.database().ref('studentplacement').child(data.key).child(xindex + ":" + yindex).once("value", snapshot => {              // snapshot             console.log("student placement is: ", snapshot.val());          });      }  } 

can please me structure trigger!?

you're waiting both functions finish before executing next bit of code. promise.all.

for (let = 1; <= section; i++) {     const xindexref = admin.database().ref('testscores').child(data.key).child('summative').child(i).once("value");     const yindexref = admin.database().ref('testscores').child(data.key).child('formative').child(i).once("value");      promise.all([xindexref, yindexref])       .then(results => {         const xsnapshot = results[0];         const ysnapshot = results[1];          return admin.database().ref('studentplacement').child(data.key).child(xsnapshot.val() + ":" + ysnapshot.val()).once("value");       })       .then(snapshot => {         console.log("student placement is: ", snapshot.val());       }); } 

promise.all waits both xindexref , yindexref complete execution.

once executed results returned thenable object.

you can access results , complete execution.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -