What is the best way to call a callback function after completion of an async call for all elements of array, from within a for
loop?
Currently I am checking for iterator to match length of array as follows, but I want to know if there is a better way.
exports.myFunction = function (arr, callback) {
for (var i = 0; i < arr.length; i++) {
(function (i) {
var comp = arr[i];
// do other calculations
// call an async function
asyncOperation(comp, function(response){
// further changes in response here
// if last array element, call callback()
if (i == arr.length - 1) {
// call the callback...
callback();
}
});
})(i);
}
}
Can you please suggest the best approach for this kind of situation?