I have a piece of javascript code doing
function asyncs(){
var backCount = 0;
function done(){
if(backCount === 2){
alert("All done");
doSomthingUseful();
}else{
backCount ++;
}
}
function asyncCall(callBack){
myRemoteCall(callBack);
}
asyncCall(done);
asyncCall(done);
asyncCall(done);
}
function doSomethingUseful(){
alert("Travel to the moon.")
}
This is working. But I am wondering if there is a better approach so that I don’t have to write this ugly counter function.
I recommend using a flow control library like async or Nimble to handle this sort of thing. Internally it does pretty much the same thing but it gives you them in reusable patterns.