There is a way to loop through an array of ids, in chunks, and wait for the response of this chunk to continue the loop?
eg:
Promise = require("bluebird")
function(array){
return Promise.*something that will loop chunks from my array and wait for each chunk response*.map(function(id){
return myfunction(id);
});
}
My problem is that I need to wait for a response from the DB that I'm accessing, and it can't answer to all calls that I'm creating at the same time. My code is raising a timeout exception because I'm being too agressive.
my code below:
Promise = require("bluebird")
function(array){
return Promise.map(array, function(id){
return myfunction(id);
});
}
EDIT: I found a solution!
Promise = require("bluebird")
function(array){
return Promise.map(array, function(id){
return myfunction(id);
}, {concurrency: 10});
}
If you are okay with not using promises, the async
module has a lot of ways to handle these scenarios. You can use eachSeries()
if you want to make one request at a time. If you want to be a bit more efficient, you can use parallelLimit
, which makes sure that max X callbacks are fired simultaneously.
There are also promise-versions of async, like async-q
and bluebird-promise ports.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With