Skip to content

Commit

Permalink
Add allKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Nov 13, 2010
1 parent 7c0d3f9 commit 44b69a4
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/promise.js
Expand Up @@ -475,6 +475,34 @@ exports.all = function(array){
return deferred.promise;
};

/**
* Takes a hash of promises and returns a promise that is fulfilled once all
* the promises in the hash keys are fulfilled
* @param hash The hash of promises
* @return the promise that is fulfilled when all the hash keys is fulfilled, resolved to the hash of results
*/
exports.allKeys = function(hash){
var deferred = new Deferred();
var array = Object.keys(hash);
var fulfilled = 0, length = array.length;
var results = {};
if (length === 0) deferred.resolve(results);
else {
array.forEach(function(key){
exports.when(hash[key],
function(value){
results[key] = value;
fulfilled++;
if(fulfilled === length){
deferred.resolve(results);
}
},
deferred.reject);
});
}
return deferred.promise;
};

/**
* Takes an array of promises and returns a promise that is fulfilled when the first
* promise in the array of promises is fulfilled
Expand Down

0 comments on commit 44b69a4

Please sign in to comment.