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 !
I think this should work:
the creator of async recomend this in the github repo (https://github.com/caolan/async/pull/85)
I would recommend using clojurescript which has an awesome core-async library which makes life super easy when dealing with async calls.
In your case, you would write something like this:
Note the
go
macro which will cause the body to run asynchronously, and the<!
function which will block until the async functions will return.The code will first block on the first async function. Then, if the result of that is true, it will run the second async function on block on that as well. Lastly, it will run the third async function and block on that.
An alternative might be:
where
f1
,f2
, andf3
are your functions.other than that, you're better off doing it explicitly, avoid making your code overly complicated, simpler is usually better
update:
Using if-async module your code will look like this:
for full example take a look here: https://github.com/kessler/if-async#example-2-using-with-asyncjs-waterfall
I am late to answer, but async-if-else might help you.
Sample Code