node.js - Javascript, NodeJS: Call Asychronous Methods in a Loop & Return Result -


is there way call async method in loop, put results in array & return results in end.

pseudo code of want do:

methodthatrunsasync(callback){   once completes, invoke callback; } 

anothermethod (){
var result = [];
for(i=1; i=10; i++){

 methodthatrunsasync(function(resp){     result.push(resp);  });  return result;    } 

}

but value of result default value. how can trap results of async block in sync block , return same caller.

looking promise framework, finding bit tough head around it. if can please me understand how achieve this, psuedo code great.

no, can't return result, calls asynchronous. use callback function too, , call when last result added:

function anothermethod (callback) {   var result = [];   var count = 10;   for(i = 0; < count; i++) {     methodthatrunsasync(function(resp){       result.push(resp);       if (result.length == count) {         callback(result);       }     });   } } 

note changed loop. loop had not iterations @ all.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -