nodejs: wait for other methods to finish before executing
javascript, node.js
Solution
To expand on my comment...
`async` is a commonly used asynchronous flow control library for Node.js.
Its `async.parallel()` would probably do well for this:
async.parallel([
function(done) {
A(function () {
done(null);
});
},
function(done) {
B(function () {
done(null);
});
}
], function (err) {
C();
});
It's possible that this can be shortened, but it depends on how each function interact with callbacks and whether they follow the common Node.js pattern of `error`-first callbacks:
async.parallel([A, B], C);
Problem
say I have 2 methods: ``` function A(callback) { ... } function B(callback) { ... } ``` I want to execute: function C(); after both A and B are finished. what we usually do is to put function C in the callback like: ``` A(function() { B(function() { C(); }); }); ``` now if both A and B takes a long time, I don't want B to execute after A has been finished. instead I want to start them at the same time to enhance performance. what I'm thinking is to implement something like a semaphore (not really a semaphore of course), it fires an event after both A and B are finished. so that I can call C from within the event. what I want to know is, is there any library implemented the above function already? I believe I'm not the first one who wants to do it. any help is appreciated.