1

I need to call ascync operations in a loop:

for (var i = 0; i < message.destinatarios.length; i++) {
  messageList.push(this.sms.send(destinatario.numero_celular, string));
 // this will take a litle time to be executed
}

// Here I need something to be fired each time one of the promises in messageList is resolved

Promise.all(messageList)
  .then(res => {
     //that is executed when all the promises have been resolved 
  })
  .catch(err => {
     // that is executed when some of then fail
  });

Then for each response I need to increment a counter like this

console.log("processing " + counter++ + " of " + messageList.length);

How would I do that in correct way since I need to wait for all promises to be fufilled until moving to the next step?

Welder Marcos
  • 129
  • 12
  • Why increment a counter if you have an index? Promise.all will give you all of the results in the order you sent them. – Kevin B Oct 26 '17 at 15:34
  • Does `this.sms.send()` return a promise? – Ozan Oct 26 '17 at 15:34
  • Yes, this.sms.send returns a promise @Ozan – Welder Marcos Oct 26 '17 at 15:35
  • I edited it @Xufox, but thats is not the real code just giving an example – Welder Marcos Oct 26 '17 at 15:37
  • @KevinB, The counter has the pourpose to be incremented when the promise is resolved. The index is for something else – Welder Marcos Oct 26 '17 at 15:40
  • 1
    @WelderMarcos Right, but if you're using promise.all, you wouldn't need to count them as they're being resolved. You would already have a callback that gets called when they're all done with all of the results. – Kevin B Oct 26 '17 at 15:40
  • @KevinB, counting them as they are finished could still be needed, apart from waiting them all with `Promise.all()`. Imagine if you needed to show a `x of y works have been completed` message to the user for example. – Ozan Oct 26 '17 at 15:45
  • @KevinB Some external process could be dependent on this counter, e.g. it should run only if more than half of that promises are resolved. I don't know... – dhilt Oct 26 '17 at 15:45
  • exactly @Ozan... – Welder Marcos Oct 26 '17 at 15:46
  • A counter like what you already have should be fine for that, assuming you fix the typo and declare the counter at a location where it won't be redeclared with each iteration. **But you didn't include that part of your code.** – Kevin B Oct 26 '17 at 15:47
  • The index is for the iteration of the message.destinatarios array. Each time the loop executes that iteration will send an ascyncronous function that will execute for some time. I want to increment the counter just after that ascyncronous function is resolved. **got it ?** @KevinB – Welder Marcos Oct 26 '17 at 15:56
  • @WelderMarcos Yes, does what you have not already do that? **we still can't see what you currently have** All you've shown us is a random console.log – Kevin B Oct 26 '17 at 15:59
  • I'll Edit to clarify @KevinB – Welder Marcos Oct 26 '17 at 16:00
  • If you're asking what I think you're asking, it's a duplicate of https://stackoverflow.com/questions/42341331/es6-promise-all-progress – Kevin B Oct 26 '17 at 16:04

2 Answers2

1

You can assign a resolveCallback to each promise. Then, use Promise.all() to wait for all your promises to do whatever work that needs to wait for all of them to finish.

let counter = 0;
for (var i = 0; i < message.destinatarios.length; i++) {
  const prom = this.sms.send(destinatario.numero_celular, string);
  messageList.push(prom);
  prom.then(() => {
    //your logic for whatever you want to do for every time a promise is resolved
    console.log("processing " + ++counter + " of " + messageList.length); 
  })
}
Promise.all(messageList)
Ozan
  • 3,709
  • 2
  • 18
  • 23
-1

Promise.all runs nested promises in parallel. You can't use counter AFAIK. At least without changing counter as a side-effect.

Alex
  • 4,621
  • 1
  • 20
  • 30