Trying to make this as general so it can help other people. This question is more me struggling with the concepts of promises.
So I have a function that returns the value of a boolean variable. My idea is to pull some data off of Firebase, and then I was looping through the values... to then set the variable to true if found.
The issue that I was running into was that the value would be returned regardless of if the ForEach loop and the query were complete. I attempted to use some sort of promise resolving (in the code snippit below) to return it properly
export function CheckIfFriendsPending(id2){
var userID = getFirebase().auth().currentUser.uid
var indicator = false
var retri = function()
{
getFirebase().database().ref('members/' + userID + '/PendingFR').once('value').then(function(snap) {
console.log("~~~~~~~~~~~~~~~~~~go")
//if(result.hasOwnProperty('install_type'))
snap.forEach(function(childSnapshot) {
var childObject = childSnapshot.val()
if(childObject.hasOwnProperty('From')){
alert("OKAY")
if(childObject.From == userID && childObject.To == id2){
indicator = true
} else if(childObject.From == id2 && childObject.To == userID){
indicator = true
}
}
});
})
} // end retri
Promise.all(retri).then(function() {
return indicator
})
}
Anyone have any idea for how to approach this? I guess a summarized version question is how to create a promise within a function that already returns a promise (which I believe the firebase database querys do). I guess it would have to wait for "ForEach" loop to resolve completely as well.
public String getKey ()The key name for the source location of this snapshot or null if this snapshot points to the database root.
A promise represents an operation and the future value it may return. It also lets you propagate errors similar to try/catch in synchronous code. You can read about promises in the Firebase SDK on The Firebase Blog, and promises in general on MDN.
Most of Firebase's APIs are asynchronous. This might be confusing at first: you're making a call, for example to fetch some data from Firestore, but you don't get a result back.
Promises are used to handle asynchronous operations in JavaScript. They are easy to manage when dealing with multiple asynchronous operations where callbacks can create callback hell leading to unmanageable code.
I think CheckIfFriendsPending
should return a Promise
:
export function CheckIfFriendsPending(id2) {
return new Promise(function (resolve, reject) {
var userID = getFirebase().auth().currentUser.uid;
var indicator = false;
getFirebase().database().ref('members/' + userID + '/PendingFR').once('value')
.then(function (snap) {
function userMatch(user) {
if (!!user.From && user.From === userID && (user.To === id2 || user.To === userID)) {
return resolve();
}
}
snap.forEach(function (childSnapshot) {
var childObject = childSnapshot.val();
userMatch(childObject);
});
return reject();
});
});
}
Then you can call:
CheckIfFriendsPending(id2)
.then(function() { console.log('Yes'); })
.catch(function() { console.log('No'); });
Here's a simple working example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With