Skip to content

Instantly share code, notes, and snippets.

@kriszyp
Created January 9, 2010 16:49
Show Gist options
  • Save kriszyp/272983 to your computer and use it in GitHub Desktop.
Save kriszyp/272983 to your computer and use it in GitHub Desktop.
/**
* 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