Conditionally call the async function in Node

I have the following code example - the first part can lead to an asynchronous call or not - in any case, this should continue. I cannot put the rest of the code in an async callback, since it should be executed when the condition is false. So how to do this?

if(condition) { someAsyncRequest(function(error, result)) { //do something then continue } } //do this next whether condition is true or not 

I assume that including code in a function as a function can be a way and calling this function in the async call above or when calling else if the condition is false - but is there an alternative way that does not require me to break it into functions?

+7
source share
4 answers

Just declare that some other function is triggered whenever you need it:

 var otherFunc = function() { //do this next whether condition is true or not } if(condition) { someAsyncRequest(function(error, result)) { //do something then continue otherFunc(); } } else { otherFunc(); } 
+3
source

The library that I used in Node quite often is Async ( https://github.com/caolan/async ). The last thing I checked also supports the browser, so you should be able to npm / concat / minify this in your distribution. If you use this only on the server side, you should consider https://github.com/continuationlabs/insync , which has slightly improved the Async version, and some of them have removed browser support.

One of the common patterns that I use when using conditional asynchronous calls is to populate the array with the functions that I want to use in order and pass them to async.waterfall.

I gave an example below.

 var tasks = []; if (conditionOne) { tasks.push(functionOne); } if (conditionTwo) { tasks.push(functionTwo); } if (conditionThree) { tasks.push(functionThree); } async.waterfall(tasks, function (err, result) { // do something with the result. // if any functions in the task throws an error, this function is // immediately called with err == <that error> }); var functionOne = function(callback) { // do something // callback(null, some_result); }; var functionTwo = function(previousResult, callback) { // do something with previous result if needed // callback(null, previousResult, some_result); }; var functionThree = function(previousResult, callback) { // do something with previous result if needed // callback(null, some_result); }; 

Of course you could use promises. In any case, I like to avoid nested callbacks using async or promises.

Some of the things you can avoid by NOT using nested callbacks are collision variables, loading errors, "marching" to the right โ†’ โ†’, hard-to-read code, etc.

+7
source

Just another way to do this, this is how I distracted the template. There may be some libraries (promises?) That handle the same thing.

 function conditional(condition, conditional_fun, callback) { if(condition) return conditional_fun(callback); return callback(); } 

And then in code you can write

 conditional(something === undefined, function(callback) { fetch_that_something_async(function() { callback(); }); }, function() { /// ... This is where your code would continue }); 
+3
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 condition (<! (someAsyncRequest))) (otherCodeToHappenWhetherConditionIsTrueOrNot)) 

Pay attention to the go macro, which will make the body work asynchronously, and the <! , which will be blocked until the async function returns. Due to the function <! that is inside the when condition, it will be blocked only if the condition is true.

0
source

All Articles