【问题标题】:how to use async.waterfall with an existing of callbacks如何将 async.waterfall 与现有的回调一起使用
【发布时间】:2014-01-01 20:19:12
【问题描述】:

我有一个命令对象数组。我需要调用 do 命令,这是一个异步调用,按顺序对每个数组元素进行。如果有任何失败,我需要停止处理。

我知道如何为个人异步调用执行 async.waterfall 调用,但我不知道如何将异步调用数组传递给 async.waterfall。

语法上不确定如何设置。

这是 Command 对象,读取函数是我需要以瀑布方式执行的异步调用...

var ICommand = require('./command');

function FabricCommand (name) {
    this.name = name;
    this.fabric = '';
    ICommand.call(this);
}

// inherit ICommand
FabricCommand.prototype = new ICommand();

FabricCommand.prototype.read = function () {
    var URL = require('url');
    var Fabric = require('./rsp_fabrics_port_status_s');
    var ResponseVerifier = require('./rsp_mgmt_rsp_s');
    var client = require('./soap_client').getInstance();

    if (client === null) {
        throw new Error('Failed to connect to SOAP server!');
    }

    var xml = '<urn:mgmtSystemGetInterfaceStatus>' +
        '<interface xsi:type=\'xsd:string\'>' + this.name + '</interface>' +
        '</urn:mgmtSystemGetInterfaceStatus>';

    client.MgmtServer.MgmtServer.mgmtSystemGetInterfaceStatus(xml, function (err, result) {
        console.log('got the response from the backend for mgmtSystemGetInterfaceStatus');

        if (err) {
            throw new Error(err);
        }

        var rs = new ResponseVerifier(result.rsp);
        if (rs.failed()) {
            throw new Error(rs.getErrorMessage())
        }

        this.fabric = new Fabric(result.rsp.portStatus.item[0]);
    });
};

【问题讨论】:

    标签: javascript node.js asynchronous node-async


    【解决方案1】:

    来自docs

    依次运行一系列函数,每个函数将其结果传递给 数组中的下一个。但是,如果任何函数传递错误 到回调,下一个函数不执行,主 回调会立即调用错误。

    编辑

    var myArray = [
        function(callback){
            callback(null, 'one', 'two');
        },
        function(arg1, arg2, callback){
            callback(null, 'three');
        },
        function(arg1, callback){
            // arg1 now equals 'three'
            callback(null, 'done');
        }
    ];
    var myCallback = function (err, result) {
       // result now equals 'done'    
    };
    
    async.waterfall(myArray, myCallback);
    
    //If you want to add multiple Arrays into the waterfall:
    var firstArray = [...],
        secondArray = [...];
    async.waterfall([].concat(firstArray,secondArray),myCallback);
    

    编辑2:

    var fabricArray = [];
    
    for (var i=0;i<10;i++){
        var fabricCommand = new FabricCommand('Command'+i);//make 10 FabricCommands
        fabricArray.push(fabricCommand.read.bind(fabricArray));//add the read function to the Array
    }
    
    async.waterfall(fabricArray,function(){/**/});
    
    //You also need to setup a callback argument
    //And a callback(null); in your code
    

    【讨论】:

    • 谢谢,正如我在问题中提到的那样,我在整个代码中都使用了如上所示的异步。这显示了如何传递一个数组,一次一个函数。我要问的是我已经有一个数组。如何将整个数组传递给瀑布调用?
    • @reza 好的。一些代码会派上用场。我会编辑我的帖子;)
    • 谢谢,这很有帮助。我仍然不清楚我需要如何使用它。所以我的数组包含带有 do 方法的 Command 对象。我需要在所有 Command 对象上调用 do 方法,如果出现异常,请为此调用处理程序。这有意义吗?
    • 请贴一些代码,这对解决您的挑战很有帮助。
    • 你有一个带有FabricCommand的数组并且需要执行read函数吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 2013-12-30
    • 2018-08-02
    • 2017-04-25
    相关资源
    最近更新 更多