Async: Pass arguments to the first function in waterfall?

Created on 25 Feb 2011  ·  9Comments  ·  Source: caolan/async

It doesn't seem to be possible to pass arguments to the first function in waterfall.

Could this be done in other ways because I really need the first one to get some variables.

Most helpful comment

async.waterfall([
    async.apply(func1, "1")
], function (err, result) {

});

function func1(par1, callback) {
    console.log(par1); // Outputs: 1
}

All 9 comments

Define a dummy function that calls your function with the variables you need. LIke this:
async.waterfall([

function(cb)
{
  cb(null, username, password);
},
Authenticate,...

Great hack =)

An alternative is to use async.apply(authenticate, username, password) to bind the arguments up front.

async.waterfall([
    async.apply(func1, "1")
], function (err, result) {

});

function func1(par1, callback) {
    console.log(par1); // Outputs: 1
}

@adisos thanks! +1

Hmmm... Great hacks, really... I too was puzzled by not being able to pass parameters to first waterfall function... But, shouldn't this be post to module author (caolan) as a bug/inconsistency ? All the neatness of that method vanishes, with these hacks... :-/

This is the intended use of these methods. async.apply was added mainly for this use case AFAIK

The documentation does give examples of using waterfall with a function, however, if anyone wants to amend the readme to show using waterfall with apply that would be fantastic

:+1: @adisos apply is great! i've only been using this library a little while and I am continually impressed.

if anyone still has this issue, the constant function works very well.

async.waterfall([
    async.constant(42),
    function (value, next) {
        // value === 42
    },
], callback);

For more info, check out http://caolan.github.io/async/docs.html#.constant

Was this page helpful?
0 / 5 - 0 ratings