Wednesday, 7 August 2013

nodejs: wait for other methods to finish before executing

nodejs: wait for other methods to finish before executing

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.

No comments:

Post a Comment