No promises, no async etc I wondered if this pattern is acceptable - my function is called and i'm passed a callback. I need to do 2 things before I can call that back:
function doSomething(..args.., callbackThatNeeds1And2DoneFirst){
var done1 = false;
var res1 = someAsync1(function call1callback(){
//blah
done1 = true;
maybeComplete();
});
var done2 = false;
var res2 = someAsync2(function call2callback(){
//blah
done2 = true;
maybeComplete();
});
function maybeComplete(){
if(done1 && done2){
callbackThatNeeds1And2DoneFirst();
}
}
}
I suppose the questions is about variable scope - can multiple "concurrent" executions interfere with each other's values for done1 and done2, or does each call get its own variable scope?
Yes, this pattern is fine. It does basically what Promise.all
does today, except that it works for exactly two asynchronous callbacks only not a variable amount.
Can multiple "concurrent" executions interfere with each other's values for
done1
anddone2
, or does each call get its own variable scope?
Every call to doSomething
creates a new scope with new done1
and done2
variables.