Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 !

like image 343
manuquentin Avatar asked Mar 14 '13 20:03

manuquentin


People also ask

What is the difference between async waterfall and async series?

However, the only difference between Waterfall and Series async utility is that the final callback in series receives an array of results of all the task whereas, in Waterfall, the result object of the final task is received by the final callback.

How does async waterfall work?

waterfall: This waterfall method runs all the functions(i.e. tasks) one by one and passes the result of the first function to the second, second function's result to the third, and so on. When one function passes the error to its own callback, then the next functions are not executed.

How do you use async parallel?

The first argument to async. parallel() is a collection of the asynchronous functions to run (an array, object or other iterable). Each function is passed a callback(err, result) which it must call on completion with an error err (which can be null ) and an optional results value. The optional second argument to async.

How does async queue work?

The async module is is designed for working with asynchronous JavaScript in NodeJS. The async. queue returns a queue object which is capable of concurrent processing i.e processing multiple items at a single time.


2 Answers

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
});
like image 118
bevacqua Avatar answered Sep 27 '22 20:09

bevacqua


I think this should work:

var finalCallback = function(err, result){
  if(err)
     // handle error..
  else
     console.log('end! :D');
}

async.waterfall(
  [
    function step1(callback){
       // stuff
       callback(null, someData);
    },
    function step2(someData, callback){
       if(skip_step_3)
          finalCallback(null, someData);
       else
          callback(null, someData);
    },
    function step3(moreData, callback){
       // more stuff
       callback(null, moreData);
    }
  ],
  finalCallback
)

the creator of async recomend this in the github repo (https://github.com/caolan/async/pull/85)

like image 24
Alejandro Silva Avatar answered Sep 27 '22 19:09

Alejandro Silva