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.