Skip to content Skip to sidebar Skip to footer

Performing Async Method In A Loop In Node.js And Waiting For Result

In the code below, how do I make console.log(output) execute only when all the asynchronous callbacks are finished? var output = []; for (var i = 0; i < x; i++) { asyncMeth

Solution 1:

Check the length of output against x if they are the same log the output

var output = [];

for (var i = 0; i < x; i++) {
    asyncMethodWithCallback(param, function(error, result){
        output.push(result);
        if(output.length == x){
            asyncComplete();
        }
    });
}

function asyncComplete(){
   console.log(output);
}

Post a Comment for "Performing Async Method In A Loop In Node.js And Waiting For Result"