简体   繁体   English

如何使用async.waterfall与现有的回调

[英]how to use async.waterfall with an existing of callbacks

I have an array of command objects. 我有一组命令对象。 I need to call the do command, this is an asynchronous call, on each of the array elements, in sequence. 我需要按顺序在每个数组元素上调用do命令,这是一个异步调用。 If any fail, I need to stop processing. 如果有任何失败,我需要停止处理。

I know how to do the async.waterfall call for individuals async calls but I can not figure out how to pass an array of asynchronous calls to async.waterfall. 我知道如何为异步调用执行async.waterfall调用,但我无法弄清楚如何将异步调用数组传递给async.waterfall。

Syntactically not sure how to set it up. 从语法上来说不确定如何设置它。

this is the Command object and the read function is the asynchronous call I need to do in a waterfall fashion... 这是Command对象,read函数是我需要以瀑布方式进行的异步调用...

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

From the docs . 来自文档

Runs an array of functions in series, each passing their results to the next in the array. 运行一系列函数,每个函数将结果传递给数组中的下一个。 However, if any of the functions pass an error to the callback, the next function is not executed and the main callback is immediately called with the error. 但是,如果任何函数将错误传递给回调,则不执行下一个函数,并立即调用主回调并显示错误。

Edit 编辑

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

Edit2: EDIT2:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM