manuquentin manuquentin - 23 days ago 8
Node.js Question

Asyncjs : Bypass a function in a waterfall chain

I want to jump a function from a chain of waterfall functions with

asyncjs
in
nodejs
.

My code look like this :

async.waterfall([
function(next){
if(myBool){
next(null);
}else{
// Bypass the 2nd function
}
},

// I want to bypass this method if myBool is false in the 1st function
function(next){
},

// Always called
function(next){
}
]);


Do you know a proper way to do this without put :

if(!myBool){
return next();
}


In the function I want to bypass.

Thanks !

Answer Source

An alternative might be:

var tasks = [f1];

if(myBool){
    tasks.push(f2);
}

tasks.push(f3);

async.waterfall(tasks, function(err, result){
});

where f1, f2, and f3 are your functions.

other than that, you're better off doing it explicitly, avoid making your code overly complicated, simpler is usually better

update:

function f1(done){
    if(myBool){
        f2(done);
    }else{
        done();
    }
}

function f2(done){
    async.nextTick(function(){
        // stuff
        done();
    });
}

async.waterfall([f1,f3],function(err,result){
    // foo
});