Last active
September 15, 2015 20:00
-
-
Save ZAYEC77/bd181e6188016476387a to your computer and use it in GitHub Desktop.
Parallel JS
This file contains hidden or 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
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