Created
March 16, 2012 11:46
-
-
Save FireyFly/2049729 to your computer and use it in GitHub Desktop.
Parallel execution of functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Calls the functions passed in `funcs` taking callbacks in parallel, calling | |
* the callback passed to `doParallel` after each of the other functions have | |
* finished. The `callback` parameter is only called once. | |
*/ | |
function doParallel(funcs, callback) { | |
var maxCount = funcs.length | |
, count = 0 | |
function f() { | |
count++; | |
if (count == maxCount) { callback() } | |
} | |
if (maxCount == 0) { return callback() } | |
funcs.forEach(function(fn) { fn(f) }) | |
} | |
doParallel([ | |
function(cb) { /*...*/; cb() }, | |
function(cb) { /*...*/; cb() } | |
], function() { | |
... | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment