var foo = function (callback_foo) {
async.series([func1, func2, func3], function (err) {
if (err) {
return callback_foo(err)
}
async.series([func4, func5], function(err){
if (err) {
return callback_foo(err)
}
return callback_foo(); //1
});
return callback_foo(); //2
});
}
我需要两次返回callback_foo()吗?第一个callback_foo()是告诉async.series func4,完成了fun5。第二个callback_foo()是告诉外部async.series func1,func2,func3完成。是吗?
答案 0 :(得分:0)
var foo = function (callback_foo) {
async.series([
func1(callback) {
//func1 processing
callback(); //this will call func2 after func1 is done
},
func2(callback) {
//func2 processing
callback(); //this will call func 3 after func 2 is done
},
func3(callback) {
//do your func3 processing here,then call async.series for 4 and 5.
async.series([
func4(callback) {
//do func 4 processing here
callback(); //This will call func5 after func4 is done
},
func5(callback) {
//do func5 processing here
callback(); //this will call the final callback of the nested async.series()
}], function (err) {
//this is the final callback of the nested(2nd) async.series call
callback(); //this is the iterator callback of func3,this will now call the final callback of the original async.series
});
}], function (err) {
//final callback after all the functions are executed.
return callback_foo();//call your foo's callback.
});
}
注意:不需要定义func1,2,3,4,5中使用的回调,它的async.series的迭代器回调,这有助于我们转移到下一个函数。 但是我没有看到嵌套的async.series调用的重点。你可以通过1个async.series调用来实现它。
var foo = function (callback_foo) {
async.series([func1, func2, func3,func4,func5], function (err) {
if (err) {
return callback_foo(err)
}
});
};