0

I'm trying to send some pushes to users when a 'invitation' is written in Realtime DB.

When the cloud function detects the creation of the invitation is triggered, and recovers the users list to ve invited (id's).

Once I get this id's I have to recover the user info for each id, in order to get some info like language, fcmTokens, etc..

Reading some SO questions I saw that I have to create a reference to the "call" like:

admin.database().ref(`/user/${userId}`).once('value')

Then store it in an array and call with Promise.all()

var userPromises = [];
for (var i = 0;i<usersInRange.length; i++) {
     var userId = usersInRange[i];
     userPromises[i] = admin.database().ref(`/user/${userId}`).once('value');
}
Promise.all(userPromises).then(results => {
...
})

And here is where I need help.

I'm having some problems , because if Promise.all() fails in retrieving some user, it stops, and don't read the other id's.

There is a way to do it "one by one" to avoid Promise.all() stop?

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
Shudy
  • 7,806
  • 19
  • 63
  • 98

1 Answers1

0

Here you can find a similar question which is already answered. Summarizing, they say that you can use an async function (if possible). Another approach is to use a loop.

function runSerial(tasks) {
    var result = Promise.resolve();
    tasks.forEach(task => {
      result = result.then(() => task());
    });
    return result;
}

It worth to take a look into it.

Joss Baron
  • 1,441
  • 10
  • 13