Skip to content Skip to sidebar Skip to footer

Why Does Returning Snapshot.val() In A Promise When Using Promise.all Not Work?

I'm writing a Firebase Cloud Function and I'm trying to figure out how Promise.all works. In my code, I pass in an array of database queries and I'm trying the read the resulting a

Solution 1:

So, here's the code that works (and the explanation below):

returnPromise.all([
    admin.database().ref(teamRef + duplicates.teamKey1).once('value'),
    admin.database().ref(teamRef + duplicates.teamKey2).once('value')
]).then(values => {
    const team1 = values[0].val();
    const team2 = values[1].val();

    console.log(team1);
    console.log(team2);
});

The reason it works is because I've always getting the promises in the values array even though I didn't know it. Here's what Promise.all returns: an array with the raw result of the promises passed in. When I was returning stuff inside the success callback, that didn't actually do anything because it wasn't part of the promise; I was just returning random stuff to an empty void. And when I was printing the teams, I was actually logging the Firebase Snapshot object instead of the .val().

Post a Comment for "Why Does Returning Snapshot.val() In A Promise When Using Promise.all Not Work?"