I have a Node.js application where multiple funcions might be called, depending on several factors, but only one last function is called after the last callback.
This is a simplified version of what I got:
if(foo === bar){
function1(arg1, function(val1){
doWhatever(val1, function(){
res.end("Finished");
});
});
}else if(foo === baz){
function2(arg2, function(val2){
doWhatever(val2, function(){
res.end("Finished");
});
});
}else{
function3(arg3, function(val3){
doWhatever(val3, function(){
res.end("Finished");
});
});
}
And this is what im doing:
var finished = false;
if(foo === bar){
function1(arg1, function(val1){
result = val1;
finished = true;
});
}else if(foo === baz){
function2(arg2, function(val2){
result = val2;
finished = true;
});
}else{
function3(arg3, function(val3){
result = val3;
finished = true;
});
}
var id = setInterval(function(){
if(finished === true){
clearInterval(id);
doWhatever(result, function(){
res.end("Finished");
});
}
}, 100);
I guess this can be simplified by using promises, however im not sure how should I implement them.
asyncmodule from github.com/caolan/async