There is a certain function, it has parameters, how to properly organize its call in a loop with different parameters (new parameters at each iteration)?

Abstract example:

while(id < N){ bd.request('users.get', {'user_id' : id}, function(_o) { console.log(_o); }); id++; } 

I do so - hangs (cycles). Tell me please.

    1 answer 1

    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){ //тут работаете с результатами }); 
    • and I have the same function with callback vk.request ('users.get', {'user_ids': 1, 'fields', 7 c c c schools ced c ced 'c:' c c c c c c c c c c c c c c c c c c c c .stringify (_o); var obj = JSON.parse (res1); console.log (obj);}); - dimaborovski