javascript - nodejs: wait for other methods to finish before executing -


say have 2 methods:

function a(callback) { ... } function b(callback) { ... } 

i want execute:
function c();
after both , b finished.
put function c in callback like:

a(function() {   b(function() {     c();   }); }); 

now if both , b takes long time, don't want b execute after has been finished. instead want start them @ same time enhance performance.
i'm thinking implement semaphore (not semaphore of course), fires event after both , b finished. can call c within event.

what want know is, there library implemented above function already? believe i'm not first 1 wants it.
appreciated.

to expand on comment...

async commonly used asynchronous flow control library node.js.

its async.parallel() this:

async.parallel([     function(done) {         a(function () {             done(null);         });     },      function(done) {         b(function () {             done(null);         });     } ], function (err) {     c(); }); 

it's possible can shortened, depends on how each function interact callbacks , whether follow common node.js pattern of error-first callbacks:

async.parallel([a, b], c); 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -