Skip to content

Instantly share code, notes, and snippets.

@ZAYEC77
Last active September 15, 2015 20:00
Show Gist options
  • Save ZAYEC77/bd181e6188016476387a to your computer and use it in GitHub Desktop.
Save ZAYEC77/bd181e6188016476387a to your computer and use it in GitHub Desktop.
Parallel JS
function Parallel() {
var self = this;
self.finished = 0;
self.length = 0;
self.list = [];
self.add = function (fn, context, params) {
params = params || [];
context = context || this;
self.list.push({fn: fn, params: params, context: context});
self.length++;
};
function callback() {
self.finished++;
if (self.finished == self.length) {
self.final();
}
}
self.start = function (cb) {
self.final = cb;
if (self.list.length){
self.list.forEach(function (item) {
var args = item.params;
args.push(callback);
item.fn.apply(item.context, args);
});
} else {
cb();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment