Question

I am attempting to dynamically load a series of anonymous functions and execute them, passing the results of the previous to the next.

Here is an example function:

module.exports = function (data) {
    // do something with data
    return (data);
}

When the functions are loaded (they all sit in separate files) they are returned as an object:

{ bar: [Function], foo: [Function] }

I would like to execute these functions using async.waterfall. This takes an array of functions, not an object of functions, so I convert as follows:

var arr =[];
        for( var i in self.plugins ) {
            if (self.plugins.hasOwnProperty(i)){
                arr.push(self.plugins[i]);
            }
        }

This gives:

[ [Function], [Function] ]

How can I now execute each function using each async.waterfall passing the result of the previous function to the next?


SOLUTION

Thanks to the comments from @piergiaj I am now using next() in the functions. The final step was to make sure that a predefined function was put first in the array that could pass the incoming data:

var arr =[];

    arr.push(function (next) {
        next(null, incomingData);
    });

    for( var i in self.plugins ) {
        if (self.plugins.hasOwnProperty(i)){
            arr.push(self.plugins[i]);
        }
    }

    async.waterfall(arr,done);
Was it helpful?

Solution

If you want them to pass data on to the next one using async.waterfall, instead of returning at the end of each function, you need to call the next() method. Additionally, you need to have next be the last parameter to each function. Example:

module.exports function(data, next){
    next(null, data);
}

The first parameter to next must be null because async.waterfall treats it as an error (if you do encounter an error in one of the methods, pass it there and async.waterfall will stop execute and finish passing the error to the final method).

You can then convert it like you already to (object to array), then call it like so:

async.waterfall(arrayOfFunctions, function (err, result) {
     // err is the error pass from the methods (null if no error)
     // result is the final value passed from the last method run    
  });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top