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 plete. 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 pletely as well.
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 plete. 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 pletely as well.
Share Improve this question edited Dec 21, 2016 at 22:38 Frank van Puffelen 599k85 gold badges889 silver badges859 bronze badges asked Dec 21, 2016 at 22:07 Joe CaraccioJoe Caraccio 2,0354 gold badges27 silver badges42 bronze badges1 Answer
Reset to default 14I 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.