Or using a third-party library, for example, async .
var async = require("async"); var calls = []; while(id < N){ calls.push(bd.request.bind(bd, 'users.get', {'user_id' : id++})); } async.series(calls, function(err, results){ //тут работаете с результатами });
Or with the help of Promise (their native support appeared in node 0.12+, for older versions you can use a third-party library)
function request(path, data){ return new Promise(function(resolve, reject){ bd.request(path, data, function(err, res){ if(err) return reject(err); resolve res; }); }); } var calls = []; while(id < N){ calls.push(request('users.get', {'user_id' : id++})); } Promise.all(calls).then(function(results){ //тут работаете с результатами });