Permalink
Browse files

Fixed all handling for empty array

  • Loading branch information...
1 parent 2e5260c commit 77e0869c2b74f9e1f0bf7d38e0aa87c227bd3f7b @kriszyp committed Jun 24, 2010
Showing with 12 additions and 9 deletions.
  1. +12 −9 lib/promise.js
View
@@ -398,16 +398,19 @@ exports.all = function(array){
}
var fulfilled = 0, length = array.length;
var results = [];
- array.forEach(function(promise, index){
- exports.when(promise, each, each);
- function each(value){
- results[index] = value;
- fulfilled++;
- if(fulfilled === length){
- deferred.resolve(results);
+ if (length === 0) deferred.resolve(results);
+ else {
+ array.forEach(function(promise, index){
+ exports.when(promise, each, each);
+ function each(value){
+ results[index] = value;
+ fulfilled++;
+ if(fulfilled === length){
+ deferred.resolve(results);
+ }
}
- }
- });
+ });
+ }
return deferred.promise;
};

0 comments on commit 77e0869

Please sign in to comment.