Node.js异步系列 - 将参数传递给下一个回调函数

43

当您使用async模块时,如何将先前回调函数中的参数传递给下一个回调函数?

以下是来自GitHub文档的示例:

async.series({
    one: function(callback){
        setTimeout(function(){
            callback(null, 1);
        }, 200);
    },
    two: function(callback){
        setTimeout(function(){
            callback(null, 2);
        }, 100);
    }
},
function(err, results) {
    // results is now equal to: {one: 1, two: 2}
});

8
如果你想将数据传递给下一个回调函数,你可能需要使用async.waterfall方法,而不是async.series - adeneo
3个回答

90

你可以使用async模块的waterfall函数来链接多个异步函数。这样你就可以说:“首先执行x,然后将结果传递给函数y,再将其结果传递给z。”引用自[文档][1]:

[1]: 文档
async.waterfall([
    function(callback){
        callback(null, 'one', 'two');
    },
    function(arg1, arg2, callback){
        // arg1 now equals 'one' and arg2 now equals 'two'
        callback(null, 'three');
    },
    function(arg1, callback){
        // arg1 now equals 'three'
        callback(null, 'done');
    }
], function (err, result) {
   // result now equals 'done'    
});

为了完成这个任务,你并不一定需要使用async模块;这个函数的设计初衷是为了让代码更易读。如果你不想使用async模块,你总可以使用传统的回调函数。


2
为什么 callback 的第一个参数总是 null - Zen
6
第一个参数是留给错误回调函数的,如果需要可以添加它们。 - AlexMA
如果结果成功,则返回 callback(null, 'done');。如果您想要为特殊情况中断流程,则返回 callback("ERROR", error_text); - Saurabh Lende

16

另一种选择是使用async.auto。使用async auto,您可以为任务指定依赖数据,当async能够运行时,它将开始运行该任务。README.md中有一个很好的示例,但以下大致是您上面提到的系列:

async.auto({
    one: function(callback){
        setTimeout(function(){
            callback(null, 1);
        }, 200);
    }, 
    // If two does not depend on one, then you can remove the 'one' string
    //   from the array and they will run asynchronously (good for "parallel" IO tasks)
    two: ['one', function(callback, results){
        setTimeout(function(){
            callback(null, 2);
        }, 100);
    }],
    // Final depends on both 'one' and 'two' being completed and their results
    final: ['one', 'two',function(err, results) {
    // results is now equal to: {one: 1, two: 2}
    }];
});

以前没见过.auto() - 太棒了! - Matt Fletcher
1
async.auto的工作链接:http://caolan.github.io/async/docs.html#auto - Nate

9

我花了相当长的时间来解决这个问题,因为我遇到了类似的情况。我尝试过async.seriesasync.waterfall两种方法。

async.series: 使用一个变量在回调函数之间共享数据。我不确定这是否是最好的方式。我必须感谢Sebastian出色的关于async的文章。

var async1 = require('async');

exports.asyncSeries1 = function (req, res, callback) {

    var sharedData = "Data from : ";
    async1.series([
            // First function
            function(callback) {
                sharedData = "First Callback";
                callback();
            },
            // Second function
            function(callback){
                console.log(sharedData);
                sharedData = "Second Callback";
                callback();
            }
        ],
        // Final callback 
        function(err) {
            console.log(sharedData);
            if (err) {
                callback();
            }
            callback();
        }
    );
};

async.waterfall: 我尝试使用async.apply来使用另一个回调函数。这是帮助我解决问题的代码片段。

var async2 = require('async')

exports.asyncWaterfall1 = function (arg1, arg2, cb) {
    async2.waterfall([
        // async.apply
        async2.apply(assignVariables, arg1, arg2),
        // First callback
        function(arg1, arg2, callback){
            console.log(arg1);
            console.log(arg2);
            arg1 = 5;
            arg2 = 6;
            callback(null, arg1, arg2);
        },
        // Second callback
        function(arg1, arg2, callback){
          // arg1 now equals 'one' and arg2 now equals 'two'
            console.log(arg1);
            console.log(arg2);
            arg1 = 7;
            arg2 = 8;
            callback(null, arg1, arg2);
        }
    ], 
    function (err, arg1, arg2) {
        console.log(arg1);
        console.log(arg2);  
    });
};

// Method to assign variables
function assignVariables(arg1, arg2, callback) {
    console.log(arg1);
    console.log(arg2);
    arg1 = 3;
    arg2 = 4;
    callback(null, arg1, arg2);
};
PS来源

两种方法都很好。我最近开始使用async。我只想问一下,在这种情况下哪种方法更好或更有效(async.series()还是async.waterfall())? - Saurabh Lende

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接