Async waterfall passing in arguments

RRP picture RRP · Mar 29, 2016 · Viewed 8.4k times · Source

I have a question regarding passing arguments in async.waterfall() to the third function rather than the first function. For example, as following

async.waterfall([
   first,
   second,
   async.apply(third, obj)
], function(err, result){});

Now is it possible to use the "obj" as an argument in the function named third and also use the arguments passed down from the callback of function named second

Answer

Deendayal Garg picture Deendayal Garg · Mar 29, 2016

Yes. You can do that. see below. see the last function.

    var async = require('async');

    async.waterfall([
        myFirstFunction,
        mySecondFunction,
        async.apply(myLastFunction, 'deen'),
    ], function (err, result) {
        console.log(result);
    });
    function myFirstFunction(callback) {
        callback(null, 'one', 'two');
    }
    function mySecondFunction(arg1, arg2, callback) {
        // arg1 now equals 'one' and arg2 now equals 'two'
        callback(null, 'three');
    }
    function myLastFunction(arg1, arg2, callback) {
        // arg1 is what you have passed in the apply function
        // arg2 is from second function
        callback(null, 'done');
    }