Skip to content Skip to sidebar Skip to footer

How To Make For Loop Wait For Firebase Database Response?

I have a function that receives an object array. The array looks like this: var receivedObjects = [{nuih329hs: 100}, {8suwhd73h: 500}, {2dsd73ud: 50}, {9u238ds: 200}]; Each key is

Solution 1:

Instead of making the loop wait, you can use a function to put currentID and currentValue into their own scope so that they don't get overwritten with each iteration:

functiongetUser(currentID, currentValue) {
    return firebase.database().ref('/users/' + currentID).once('value').then(function(child) {
        console.log("The current ID is: "+currentID+" and the current value is: "+currentValue);
    });
}

In your loop:

for (var i = 0; i < receivedObjectsLength; i++) {
    ...
    getUser(currentID, currentValue)
}

Post a Comment for "How To Make For Loop Wait For Firebase Database Response?"