I have an asynchronous function that works with the result of two other async functions.
Till now what I was doing is that I write function2 in the callback function1 and function2 in the callback of function2
function1(callbackFunction() {
function2(callbackFunction() {
function3()
})
})
Is there any other way to handle this. I work usually with JavaScript code in client side and in nodeJs.
My scenario is that for function2 I don't need output from function1. In another words function1 and function2 are independent; but function3 is dependent on function1 and function2.
I want my function2 to run independent on function1 but function3 to run dependent on both functio1 and function2.
Is there anything like
function1();
function2();
when(funtion1plete && funtion2plete) {
function3();
}
I have an asynchronous function that works with the result of two other async functions.
Till now what I was doing is that I write function2 in the callback function1 and function2 in the callback of function2
function1(callbackFunction() {
function2(callbackFunction() {
function3()
})
})
Is there any other way to handle this. I work usually with JavaScript code in client side and in nodeJs.
My scenario is that for function2 I don't need output from function1. In another words function1 and function2 are independent; but function3 is dependent on function1 and function2.
I want my function2 to run independent on function1 but function3 to run dependent on both functio1 and function2.
Is there anything like
function1();
function2();
when(funtion1.plete && funtion2.plete) {
function3();
}
Share
Improve this question
edited May 20, 2014 at 3:08
Okky
asked May 20, 2014 at 2:52
OkkyOkky
10.5k15 gold badges77 silver badges123 bronze badges
3
- 1 You've got the right idea. If you get tired of more and more nesting, take a look at the async.js library in npm. What you have is handled by the "series" helper in async.js github./caolan/async#seriestasks-callback – Paul Commented May 20, 2014 at 2:59
- @Paul I have made a few changes in the question, could you consider that too. – Okky Commented May 20, 2014 at 3:09
- 1 Looks like Jacob wrote you a great answer already...! – Paul Commented May 20, 2014 at 3:14
2 Answers
Reset to default 6There are some good libraries to deal with orchestrating asynchronous functions. async
and q
(or other Promises/A libraries) help.
If function2
does not depend on the result of function1
, you can execute them in parallel. Here's how it looks using async
(these examples assume that your callback has a function(err, result)
signature, which is the defacto pattern 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 from function1
, waterfall
may be a better pattern:
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 pass promises around and do all kinds of nifty stuff. Here's how this would look using that:
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 arbitrary order:
q.all([q.nfcall(function1), q.nfcall(function2)])
.then(function(values) { function3(values[0], values[1]); })
.fail(function(err) {
});
Here is a solution that I baked. You can try a call manager to call the dependant 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();
With the current config provided above, when it is run the output is given 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