0

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]);
    });
};
Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
reza
  • 5,972
  • 15
  • 84
  • 126

1 Answers1

2

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
A1rPun
  • 16,287
  • 7
  • 57
  • 90
  • thanks, I use async as shown above throughout my code as I mentioned in my question. This shows how to pass an array, one function at a time. What I am asking is I already have an array. How do I pass the whole array into the waterfall call? – reza Dec 13 '13 at 22:43
  • @reza Alright. Some code will came in handy. I'll edit my post ;) – A1rPun Dec 13 '13 at 22:45
  • Thank you this is helpful. I am still unclear on how I need to use this. So my array contains Command objects with do method. I need to call the do method on all the Command objects and in case of exception, call the handler for that. Does this make any sense? – reza Dec 13 '13 at 23:17
  • Please post some code, that will be very helpfull to solve your challenges. – A1rPun Dec 13 '13 at 23:20
  • Do you have an Array with `FabricCommand` and need to execute the `read` function? – A1rPun Dec 13 '13 at 23:51
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43159/discussion-between-reza-and-a1rpun) – reza Dec 14 '13 at 00:08
  • Thank you A1rPun for all of your help. The async.waterfall(fabricArray,function(){/**/}); was key and also the piece about fabricCommand.read.bind(fabricCommand) was needed for async waterfall call the apply function on my FabricCommand object. – reza Dec 16 '13 at 17:03