Tuesday, 3 December 2019

Promise wait

https://stackoverflow.com/questions/35862294/bluebird-js-how-to-add-wait-for-each-iteration-of-promise-map

Bluebird.js: How to add wait for each iteration of Promise.map?




3
because the downstream has a limit on the number of requests
The solution to this type of problem is to limit the number of simultaneous requests that you make. Using a delay would just be a guess as to how to control that, but not precise at all. Instead, you should literally limit the number of requests you have in flight at the same time.
How can I achieve the same ?
Fortunately for you, Bluebird has a concurrency option exactly for controlling how many requests are in-flight at the same time with the Promise.map() method by setting the concurrency option. Let's say you found that it was safe to have two requests in flight at the same time. Then, you could do this:
return Promise.map(array, function (elem){
    // perform an async call, return a promise from that async call
}, {concurrency: 2});
This will manage the iteration through the array so that no more than 2 async operations are in flight at the same time. You can obviously set concurrency to whatever value you find appropriate.
The Promise.mapSeries() method is a special case of Promise.map() with concurrency already set to 1. So, if you truly wanted the calls to be sequential (with only one in-flight at any given time), then you could just use the Promise.mapSeries() method instead.

No comments:

Post a Comment