:
asyncFunc1(arguments, function(err, data){
if(err) throw err;
});
function asyncFunc1(arguments, callback) {
asyncFunc2(arg, function(err, data) {
if(err) return callback(err);
callback(null, result);
});
}
. fs.readFile();
- , , - :
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 asynchronous calls you made, and calls _callbackwhen everything is done. But you need to add a callback to the methods XHR().success().
source
share