How to handle an async function, which depends on several other async functions

I have an asynchronous function that works with the result of two other asynchronous functions.

So far, what I have been doing, I am writing function2 in function callback1 and function2 in callback function2

function1(callbackFunction() { function2(callbackFunction() { function3() }) }) 

Is there any other way to handle this. I usually work with JavaScript code on the client side and in nodeJs.

My scenario is that for function2, I don't need the output of function1. In other words, function 1 and function 2 are independent; but function 3 depends on function1 and function2.

I want my function2 to be executed independently of function1, but function3 would run depending on functio1 and function2.

Is there anything like

 function1(); function2(); when(funtion1.complete && funtion2.complete) { function3(); } 
+7
javascript asynchronous
source share
2 answers

There are some good libraries for handling asynchronous functions. async and q (or other Promises / A libraries).

If function2 does not depend on the result of function1 , you can execute them in parallel. Here's what it looks like with async (in these examples, it is assumed that your callback has the signature function(err, result) , which is the defacto template for Node:

 async.parallel([ function(callback) { function1(callback); }, function(callback) { function2(callback); } ], function(err, values) { function3(values[0], values[1]); }); 

If function2 depends on the result of function1 , waterfall might be the best template:

 async.waterfall([ function(callback) { function1(callback); }, function(result, callback) { function2(result, callback); }, function(result, callback) { function3(result, callback); }, ]); 

Personally, I like q because you can go promises around and do all kinds of great things. Here's what it would look like with this:

 q.nfcall(function1) .then(function(result) { return q.nfcall(function2); }) .then(function(result) { return q.nfcall(function3); }) .fail(function(err) { // If any of them fail, this is called. }); 

Or, if function1 and function2 can be done in random order:

 q.all([q.nfcall(function1), q.nfcall(function2)]) .then(function(values) { function3(values[0], values[1]); }) .fail(function(err) { }); 
+6
source share

Here is the solution I baked. You can try the call manager to call dependent functions

 var func1 = function() { console.log("Dependant call ... " + 1); }; var func2 = function() { console.log("Dependant call ... " + 2); }; var func3 = function() { console.log("Dependant call ... " + 3); }; var func4 = function() { console.log("Dependant call ... " + 4); }; var CallManager = function(funcs_config) { var _this = this; _this.functions = funcs_config; _this.callAsynchronous = function(f) { if (f != undefined) { for (var i = 0; i < f.length; i++) { f[i].call(function() { this.callAsynchronous(f.splice(0,1)); }); } return; } for (var func in _this.functions) { if (_this.functions[func].length == 0) { //not dependent to any function } else { console.log('Calling....' + func); _this.callAsynchronous(_this.functions[func]); eval(func +'();'); } } }; return _this; }; var callManager = new CallManager({ //dependency configuration func2: [func1], //func2 depends on func1 func3: [func2], func4: [func1, func3] //func4 depends on both func1 and func3 }); callManager.callAsynchronous(); 

If the current configuration given above, when it starts, is issued as -

 Calling....func2 Dependant call ... 1 Dependant call ... 2 Calling....func3 Dependant call ... 2 Dependant call ... 3 Calling....func4 Dependant call ... 1 Dependant call ... 3 Dependant call ... 4 
+2
source share

All Articles