Skip to content

Instantly share code, notes, and snippets.

@FireyFly
Created March 16, 2012 11:46
Show Gist options
  • Save FireyFly/2049729 to your computer and use it in GitHub Desktop.
Save FireyFly/2049729 to your computer and use it in GitHub Desktop.
Parallel execution of functions
/**
* 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