I have a function foo which makes multiple (parallel) asynchronous calls in a loop. I need to somehow wait until the results of all of the calls are available.
A simple way of doing it would be to trigger a callback once all responses are in the array:
function foo(cb) {
var results = [];
for (var i = 0; i < 10; i++) {
someAsyncFunction({someParam:i}, function callback(data) {
results.push(data);
if(results.length===10){
cb(results);
}
});
}
}
foo(function(resultArr){
// do whatever with array of results
});
Only difference from the Promise.all approach is the order of the results is not guaranteed; but that is easily achievable with a few additions.