A popular pattern for async methods are.
asyncFunc1(arguments, function(err, data){
if(err) throw err;
// Do your stuff
});
function asyncFunc1(arguments, callback) {
asyncFunc2(arg, function(err, data) {
if(err) return callback(err);
// Do your stuff
callback(null, result);
});
}
See for an example fs.readFile();
Edit
Still using callbacks but not that particular pattern you could use something like this:
function do(x,y,_callback){
var count = 0;
if(x){
count++;
XHR1().success(checkDone);
}
if(y){
count++;
XHR2().success(checkDone);
}
function checkDone(){
count--;
if(!count) _callback();
}
}
function done(){
return something;
}
do(x=1,y=1,done());
The counter keeps track of how many async calls you have made and calls the _callback
when all are done. But you need to add a callback to the XHR().success()
methods.