Skip to content

Instantly share code, notes, and snippets.

@kputnam
Created November 4, 2011 22:05
Show Gist options
  • Save kputnam/1340612 to your computer and use it in GitHub Desktop.
Save kputnam/1340612 to your computer and use it in GitHub Desktop.
Group of Asynchronous Workers
/**
* Starts all workers at once and executes the given afterFinish
* callback when all workers have completed. Workers are expected
* to signal when they are finished.
*
* @example
*
* WorkerGroup(
* [ function(whenDone) { $.ajax({url: 'http://jquery.com/a/', error: whenDone, success: whenDone}); }
* , function(whenDone) { $.ajax({url: 'http://jquery.com/b/', error: whenDone, success: whenDone}); }
* , function(whenDone) { $.ajax({url: 'http://jquery.com/c/', error: whenDone, success: whenDone}); } ],
* function(returnVals) { console.log('results are in:', returnVals); });
*
* // WorkerGroup returns immediately and execution resumes here.
* console.log('collecting data');
*/
function WorkerGroup(workers, afterFinish) {
var activeCount = workers.length;
var returnVals = [];
var observeWorker = function(workerId) {
// Called by a worker when it's finished
return function(returnVal) {
returnVals[workerId] = returnVal;
if (-- activeCount == 0 && afterFinish)
afterFinish(returnVals);
};
};
for (var n = 0; n < workers.length; n ++) {
(function(workerId) {
setTimeout(function() {
// Hmm... this worker may punt by calling setTimeout,
// so we can't really know if he finishes unless he
// calls the given observeWorker value
workers[workerId](observeWorker(workerId));
}, 0);
})(n);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment