Skip to content
This repository has been archived by the owner on May 10, 2020. It is now read-only.

Commit

Permalink
resolving when immediately on empty array
Browse files Browse the repository at this point in the history
  • Loading branch information
ab-5v committed Mar 16, 2013
1 parent 3f7244d commit c55cce7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pzero",
"version": "0.3.0",
"version": "0.3.1",
"description": "Small promise lib",
"keywords": [ "promise", "deferred", "flow" ],
"author": {
Expand Down
32 changes: 18 additions & 14 deletions pzero.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,20 +172,24 @@ pzero.when = function(promises) {
var result = [];
var len = promises.length;

promises.forEach(function(current, i) {
current
.then(function(value) {
result[i] = value;
len--;

if (!len) {
promise.resolve(result);
}
})
.esle(function(value) {
promise.reject(value);
});
});
if (len) {
promises.forEach(function(current, i) {
current
.then(function(value) {
result[i] = value;
len--;

if (!len) {
promise.resolve(result);
}
})
.esle(function(value) {
promise.reject(value);
});
});
} else {
promise.resolve([]);
}

return promise;
};
Expand Down
4 changes: 4 additions & 0 deletions test/spec/pzero.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ describe('promise', function() {
})
});

it('should not wait for empty array of promises', function(done) {
pzero.when([]).then(function() { done(); });
});

it('should wait for promise', function(done) {
pzero.when([this.promise]).then(function(data) {
expect(data[0]).to.eql(1);
Expand Down

0 comments on commit c55cce7

Please sign in to comment.