I'm trying to write a function that calls an async function in a loop, and only when the loop has finished, to call the callback with the result.
Something like this:
function foo(cb){
var result;
for(var i=0; i < 999; i++){
asyncFunction(i, function(val){
do something with result
});
}
cb(result);
}
But I see that it gets to cb(result); before it actually finishes returning from all the asyncFunction calls.
What can be done to make it wait to finish the loop before calling cb? Will a promise help?