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) {
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) { });