5

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Joe Caraccio
  • 1,899
  • 3
  • 24
  • 41

1 Answers1

14

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.

adrice727
  • 1,482
  • 12
  • 17