Firebase Promises
Firebase Nodejs requests are Asyncronus .Which means, in the follwing code block I need to check order users exhist before set user.
But the user order insert fucntion written second will fire before validation finished .Developer need to careful on this when firebase adopted.
for(var user in users) {
firebase.database().ref("users"); users.once("value", function(snapshot) {
var temp= snapshot.exists();
if (temp) {
var temp= on_exhist_checked(snapshot);
}
});
}
function on_exhist_checked(snapshot) {
var temp= firebase.database().child(snapshot.key).child('orders').set({
key: true
});
return temp;
}
However Firebase Promises has make the life easy .You just need to use "then"
for(var user in users) {
firebase.database().ref("users"); users.once("value").then(function(snapshot) {
var temp= snapshot.exists();
// if (temp) {
// var temp= on_exhist_checked(snapshot);
// }
// });
// }
But the user order insert fucntion written second will fire before validation finished .Developer need to careful on this when firebase adopted.
for(var user in users) {
firebase.database().ref("users"); users.once("value", function(snapshot) {
var temp= snapshot.exists();
if (temp) {
var temp= on_exhist_checked(snapshot);
}
});
}
function on_exhist_checked(snapshot) {
var temp= firebase.database().child(snapshot.key).child('orders').set({
key: true
});
return temp;
}
However Firebase Promises has make the life easy .You just need to use "then"
for(var user in users) {
firebase.database().ref("users"); users.once("value").then(function(snapshot) {
var temp= snapshot.exists();
// if (temp) {
// var temp= on_exhist_checked(snapshot);
// }
// });
// }
Comments
Post a Comment