Javascript 在最终回调中嵌套异步函数是如何工作的?

Javascript 在最终回调中嵌套异步函数是如何工作的?,javascript,node.js,asynchronous,Javascript,Node.js,Asynchronous,我需要返回回调函数\u foo()两次吗?第一个回调函数_foo()告诉async.series func4,fun5已完成。第二个回调函数_foo()告诉外部异步。系列func1、func2、func3已完成。是吗?你可以像下面这样做 var foo = function (callback_foo) { async.series([func1, func2, func3], function (err) { if (err) {

我需要返回回调函数\u foo()两次吗?第一个回调函数_foo()告诉async.series func4,fun5已完成。第二个回调函数_foo()告诉外部异步。系列func1、func2、func3已完成。是吗?

你可以像下面这样做

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
        });
}
注意:func1,2,3,4,5中使用的回调不需要定义,它是async.series的迭代器回调,这有助于我们转到下一个函数。 但是,我看不到嵌套async.series调用的意义。您可以使用1个async.series调用来实现

 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.
        });
    }

看看这个答案是否对你有帮助:
var foo = function (callback_foo) {
    async.series([func1, func2, func3,func4,func5], function (err) {
        if (err) {
            return callback_foo(err)
        }
    });
};