Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bluebird - How to loop through array in chunks, waiting for a chunk response

Tags:

node.js

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});
}
like image 907
Rodrigo Ney Avatar asked Oct 31 '22 20:10

Rodrigo Ney


1 Answers

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.

like image 141
William Avatar answered Nov 15 '22 05:11

William