Asyncjs: function bypass in a waterfall chain

I want to rescan a function from a waterfall function chain from asyncjs to nodejs .

My code looks 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){ } ]); 

You know the right way to do this without put:

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

In the function I want to get around.

Thanks!

+6
source share
5 answers

An alternative could 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.

besides this, you are better off doing this explicitly, avoiding over-complicating the code, easier, 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 }); 
+6
source

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 ) 

async creator recommends this on github repo ( https://github.com/caolan/async/pull/85 )

+8
source

Using if-async , your code will look like this:

 var async = require('async') var ifAsync = require('if-async') async.waterfall([ foo, ifAsync(p1).then(c1).else(c2), bar ], function(err) {}) 

for a complete example see here: https://github.com/kessler/if-async#example-2-using-with-asyncjs-waterfall

+1
source

I'm late, but async-if-else can help you.

Code example

  var async = require('async-if-else')(require('async')); function emailExists(user, callback) { user.find(user.email, function(err, dbUser){ if (err) return callback(error); if(!dbUser) return callback(null, false); // does not exist, predicate will be false callback(null, true); }); } function updateAccount(user, callback) { user.update( ..., callback); } function importFromLegacyByEmail(user, callback) { remoteClient.get(user, callback); } async.waterfall([ async.constant({email: ' thiago@email.com ', dogs: 2, money: 0, fun: 100 }), async.if(emailExists, updateAccount).else(importFromLegacyByEmail), sendEmail ], handler); 
0
source

I would recommend using clojurescript , which has awesome core-async , which makes life easier when working with asynchronous calls.

In your case, you should write something like this:

 (go (when-let [res1 (<! (asyncFunc1))] (<! (asyncFunc2 res1))) (<! (asyncFunc3))) 

Pay attention to the go macro, which will make the body work asynchronously, and the <! , which will be blocked until async functions are returned.

First, the code will block at the first asynchronous function. Then, if the result of this is true, it will also run the second asynchronous function in the block. Finally, it will run the third async function and block it.

-4
source

All Articles