Working with an asynchronous language (node.js in my case) I often end up with a two dimensional loop and an response at the very end:
firstArray.forEach(function(first){
sencondArray[first].forEach(function(second){
doStuff(function callback(resultSum){
if(isLastCallback(this))
response.send(resultSum);
});
});
});
My first approach to get the proper result of isLastCallback, was using the provided counter i of both forEach iterations, but that ended up in a huge mess.
What is the common way to detect if the current run of callback is the last one?
I think what you're looking for is to do several asynchronous calls and then combine the results. You should look at the async module. You probably want async#parallel. It will run your async functions in parallel and then call the final callback with an array of all the results.