Async waterfall passing in arguments
Asked Answered
L

1

10

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

Lure answered 29/3, 2016 at 18:33 Comment(0)
J
30

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');
    }
Jamarjamb answered 29/3, 2016 at 19:7 Comment(1)
Thank you! this should be in the lib documentation :)Polymerism

© 2022 - 2024 — McMap. All rights reserved.