javascript-sync-async-foreach
javascript-sync-async-foreach copied to clipboard
control does not wait for asynchrounous calls in foreach to complete
I am trying to use this in an API call:
forEach(["a", "b", "c"], function(item, index, arr) {
var done = this.async();
console.log("each", item, index, arr);
setTimeout(function() {
done();
}, 500);
}, function(){
console.log("all done demo")
}
);
console.log("END OF FOREACH")
here's what console looks like:
each a 0 [ 'a', 'b', 'c' ]
END OF FOREACH //as soon as it reaches this point, it returns to the calling function.
each b 1 [ 'a', 'b', 'c' ]
each c 2 [ 'a', 'b', 'c' ]
I essentially wanted to calculate some values in the foreach callback and return to calling the function from there. How do I get that working?