reza
reza

Reputation: 6358

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. 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.

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...

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

Upvotes: 0

Views: 1942

Answers (1)

A1rPun
A1rPun

Reputation: 16837

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:

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

Upvotes: 2

Related Questions