Skip to content

Instantly share code, notes, and snippets.

@tim-smart
Created March 18, 2010 05:12
Show Gist options
  • Select an option

  • Save tim-smart/336068 to your computer and use it in GitHub Desktop.

Select an option

Save tim-smart/336068 to your computer and use it in GitHub Desktop.
var Parallel = exports.Parallel = function Parallel(actions) {
if ('object' === typeof actions) {
var keys = Object.keys(actions);
for (var i = 0, key; key = keys[i++]; ) {
this.add(key, actions[key]);
}
}
return this;
};
Parallel.prototype.actions = {};
Parallel.prototype.add = function add(name, action) {
this.actions[name] = actions;
};
Parallel.prototype.run = function run(callback) {
var count = 0,
action,
args,
keys = Object.keys(this.actions);
for (var i = 0, key; key = keys[i++]; ) {
action = this.actions[key][0];
args = this.actions[key].slice(1);
args.push((function(name, action) {
return function() {
arguments = Array.prototype.slice(arguments);
arguments.unshift(name);
callback.apply(global, arguments);
count--;
if (0 >= count) {
callback(null);
}
};
})(key, action));
count++;
action.apply(global, args);
}
if (0 >= count) {
callback(null);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment