Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async waterfall passing in arguments

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

like image 317
RRP Avatar asked Mar 29 '16 18:03

RRP


1 Answers

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');
    }
like image 136
Deendayal Garg Avatar answered Sep 28 '22 05:09

Deendayal Garg