1

My nodejs code has a loop that calls a Promise-returning function:

collection.forEach((item) => {
    promiseReturningFunction(item);
});

The problem is, the backend that is receiving these calls is not that great at handling concurrent requests.

So I want to slow down this loop, each turn waiting for the previous promise to finish. Effectively making it synchronous.

Any ideas?

Nathan H
  • 48,033
  • 60
  • 165
  • 247
  • Do you have access to `async` + `await`? – christopher Sep 05 '17 at 11:43
  • Check [**this**](https://stackoverflow.com/a/45349880/5452965) as a possible idea, you can take the code and modify it to fit your needs – codtex Sep 05 '17 at 11:44
  • You have several options: combine array functions with the Promise API (still smells like async), use some vendor Promise API (i.e. [bluebird](http://bluebirdjs.com/docs/api-reference.html) - still smells a bit async), use [`coroutines`](http://bluebirdjs.com/docs/api/promise.coroutine.html) with Generators (Node > 4), use `async/await` (Node > 7.5). In my experience the `coroutines` + Generator worked pretty well with dev coming from Python. – MarcoL Sep 05 '17 at 12:52

0 Answers0