Node.js嵌套Async.Waterfall()函数中的回调

时间:2015-12-24 03:50:29

标签: node.js asynchronous callback nested

我正在尝试在另一个使用async.waterfall()的函数中运行使用async.waterfall()的命名函数。

如何构造回调,以便将内部函数的输出传递给外部函数?

以下代码不使用嵌套的异步并且工作正常:

//nested_async_1.js

var async = require("async");

console.log("Script running");

// Inner function, to be called by the outer function
var InnerAsync = function(callback_inner_async) {

    async.waterfall([
        function (callback) {
            console.log("Inner function 1");
            callback(null, "results 1")
        },

        function (input_1, callback){
            console.log("function 2 " + input_1);
            callback(null, "results 2")
        }
    ],
    function (err, final_result) {
        if (err) {
            console.log("Server error: ", err);
            callback(err)
        }
        else{
            var return_val = "Final result " + final_result;
            console.log(return_val);
            callback_inner_async(return_val);
        }
    });
};


// Outer function
InnerAsync(function(result){
    console.log("Outer function " + result);
});

输出

C:\Data\>node nested_async_1.js  
Script running  
Inner function 1  
function 2 results 1  
Final result results 2  
Outer function Final result results 2  

虽然以下嵌套异步代码未运行:

//nested_async_2.js

var async = require("async");

console.log("Script running");

// Inner function, to be called by the outer function
var InnerAsync = function(callback_inner_async) {

    async.waterfall([
        function (callback) {
            console.log("Inner function 1");
            callback(null, "results 1")
        },

        function (input_1, callback){
            console.log("function 2 " + input_1);
            callback(null, "results 2")
        }
    ],
    function (err, final_result) {
        if (err) {
            console.log("Server error: ", err);
            callback(err)
        }
        else{
            var return_val = "Final result " + final_result;
            console.log(return_val);
            callback_inner_async(return_val);
        }
    });
};

// Outer function, which calls the inner function
async.waterfall([
    function(callback_outer){
        console.log("Outer function 1");
        callback_outer(null, "Outer result 1")
    },
    function(input_1, callback_outer){
        console.log("Outer function 2");
        // Run the inner function, and get the results
        // How should this be structured???
        callback_outer(null, InnerAsync())
    }
],
    function(err, final_outer_result){
        if(err){
            console.log("Error on outer function: " + err)
        }
        else {
            console.log("Final outer function");
            console.log(final_outer_result);
        }
    }
);

输出

C:\ Data> node nested_async_2.js
脚本运行
外部功能1
外部功能2
内部功能1
功能2结果1
最终外部功能
未定义
最终结果2
C:\数据\ nested_async_2.js:31
            callback_inner_async(RETURN_VAL);
            ^

TypeError:callback_inner_async不是函数
    在C:\ Data \ nested_async_2.js:31:13
    在C:\ Data \ node_modules \ async \ lib \ async.js:52:16
    在Immediate._onImmediate(C:\ Data \ node_modules \ async \ lib \ async.js:1201:34)
    at processImmediate [as _immediateCallback](timers.js:374:17)

2 个答案:

答案 0 :(得分:1)

而不是

function(input_1, callback_outer){
    console.log("Outer function 2");
    // Run the inner function, and get the results
    // How should this be structured???
    callback_outer(null, InnerAsync())
}

应该是

function(input_1, callback_outer){
    console.log("Outer function 2");
    InnerAsync(function(result) {
        console.log("Result from InnerAsync: ", result);
        callback_outer(null, result);
    });
}

按预期工作。在nested_async_2.js中,您在没有回调的情况下拨打InnerAsync,因此在尝试呼叫时失败。

InnerAsync中还有一些错误处理问题。该函数不遵循error-first callback模式,作为相关的事情,代码将在这些行上失败

function (err, final_result) {
    if (err) {
        console.log("Server error: ", err);
        callback(err) // will fail here
    }

(如果发生错误),因为那里没有定义callback

答案 1 :(得分:0)

我认为你在没有任何争论的情况下调用 InnerAsync ; 因此,在调用 InnerAsync 时,您没有传递回调函数 callback_inner_async ,而您正在收到错误。

试试这个:

async.waterfall([
    function(callback_outer){
        console.log("Outer function 1");
        callback_outer(null, "Outer result 1")
    },
    function(input_1, callback_outer){
        console.log("Outer function 2");

        // Call InnerAsync with the callback function

        InnerAsync(callback_outer);
    }
],
    function(err, final_outer_result){
        if(err){
            console.log("Error on outer function: " + err)
        }
        else {
            console.log("Final outer function");
            console.log(final_outer_result);
        }
    }
);