Created
January 9, 2010 16:49
-
-
Save kriszyp/272983 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Takes an array of promises and returns a promise that that is fulfilled once all | |
* the promises in the array are fulfilled | |
* @param group The array of promises | |
* @return the promise that is fulfilled when all the array is fulfilled | |
*/exports.group = function(group){ | |
var deferred = defer(); // this function is assigned process.Promise | |
if(!(group instanceof Array)){ | |
group = Array.prototype.slice.call(arguments); | |
} | |
var fulfilled, length = group.length; | |
var results = []; | |
group.forEach(function(promise, index){ | |
exports.when(promise, function(value){ | |
results[index] = value; | |
fulfilled++; | |
if(fulfilled === length){ | |
deferred.resolve(results); | |
} | |
}, | |
deferred.reject); | |
}); | |
return deferred.promise; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment