Created
May 2, 2010 04:23
-
-
Save coolaj86/386892 to your computer and use it in GitHub Desktop.
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
// join any number of promises and return the results in the order they were passed in | |
function join_promises () { | |
var p = make_promise(), | |
args = Array.prototype.slice.call(arguments,0), | |
num = 0, | |
ps = []; | |
// Debug version only | |
if (args[0].isArray) { | |
if (args.length > 1) { | |
var msg = "Multiple arrays of promises not supported"; | |
DBG(msg); | |
throw msg; | |
} | |
//args = args[0]; | |
} | |
// Production version | |
args = (args[0].isArray ? args[0] : args); | |
num = args.length; | |
args.forEach(function (el, i, arr) { | |
// increase the array to the appropriate size | |
ps.push(undefined); | |
el.when(function (data) { | |
ps[i] = data; | |
num -= 1; | |
DBG(JSON.stringify(args) + ' ' + ps[i] + ' ' + num + ' ' + i ); | |
if (0 === num) { | |
DBG('Result: ' + JSON.stringify(ps)); | |
p.fulfill.apply(p, ps); | |
// p.fulfill.call(p, ps); | |
} | |
}); | |
}); | |
return p; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment