Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

resolving when immediately on empty array

  • Loading branch information...
commit c55cce798b65ef049baec0ce8ab3b4539bfd8a31 1 parent 3f7244d
@artjock authored
Showing with 23 additions and 15 deletions.
  1. +1 −1  package.json
  2. +18 −14 pzero.js
  3. +4 −0 test/spec/pzero.js
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "pzero",
- "version": "0.3.0",
+ "version": "0.3.1",
"description": "Small promise lib",
"keywords": [ "promise", "deferred", "flow" ],
"author": {
View
32 pzero.js
@@ -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;
};
View
4 test/spec/pzero.js
@@ -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);
Please sign in to comment.
Something went wrong with that request. Please try again.