Created
October 13, 2012 15:47
-
-
Save rezoner/3885077 to your computer and use it in GitHub Desktop.
Asynchronous parallel - different approach
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
/* usage: | |
parallel( | |
func1, [arg, arg, arg...], | |
func2, [arg, arg, arg...], | |
... | |
function() { | |
arguments[0]; // holds output from first function | |
arguments[1]; // holds output from second function | |
} | |
http://jsfiddle.net/8nF3g/ | |
*/ | |
parallel = function() { | |
var onReady = arguments[arguments.length - 1]; | |
var ready = 0; | |
var results = []; | |
var count = (arguments.length - 1) / 2; | |
for (var i = 0; i < arguments.length - 1; i += 2) { | |
var func = arguments[i]; | |
var args = arguments[i + 1]; | |
var f = function foo() { | |
var i = arguments[arguments.length - 1]; | |
results[foo.i / 2] = arguments; | |
ready++; | |
if (ready == count) { | |
onReady.apply(null, results); | |
} | |
} | |
f.i = i; | |
args.push(f); | |
func.apply(null, args) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment