Skip to content

Commit

Permalink
Consistently reject with a RangeError when input array contains too f…
Browse files Browse the repository at this point in the history
…ew promises. Fixes #233
  • Loading branch information
petkaantonov committed Jul 7, 2014
1 parent ecd6600 commit 520d344
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
18 changes: 14 additions & 4 deletions src/some.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ SomePromiseArray.prototype._init = function SomePromiseArray$_init() {
this._resolve([]);
return;
}
this._init$(void 0, RESOLVE_ARRAY);
this._init$(void 0, RESOLVE_CALL_METHOD);
var isArrayResolved = isArray(this._values);
if (!this._isResolved() &&
isArrayResolved &&
this._howMany > this._canPossiblyFulfill()) {
var message = "(Promise.some) input array contains less than " +
this._howMany + " promises";
this._reject(new RangeError(message));
this._reject(this._getRangeError(this.length()));
}
};

Expand Down Expand Up @@ -107,6 +105,18 @@ function SomePromiseArray$_canPossiblyFulfill() {
return this.length() - this._rejected();
};

SomePromiseArray.prototype._getRangeError =
function SomePromiseArray$_getRangeError(count) {
var message = "Input array must contain at least " +
this._howMany + " items but contains only " + count + " items";
return new RangeError(message);
};

SomePromiseArray.prototype._resolveEmptyArray =
function SomePromiseArray$_resolveEmptyArray() {
this._reject(this._getRangeError(0));
};

function Promise$_Some(promises, howMany) {
if ((howMany | 0) !== howMany || howMany < 0) {
return apiRejection(POSITIVE_INTEGER_ERROR);
Expand Down
14 changes: 6 additions & 8 deletions test/mocha/when_any.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,16 @@ function contains(arr, result) {
return arr.indexOf(result) > -1;
}

var RangeError = when.RangeError;


describe("when.any-test", function () {

specify("should resolve to empty array with empty input array", function(done) {
specify("should reject on empty input array", function(done) {
var a = [];
when.any(a).then(
function(result) {
assert(result !== a);
assert.deepEqual(result, []);
done();
}, fail
);
when.any(a).caught(RangeError, function() {
done();
});
});

specify("should resolve with an input value", function(done) {
Expand Down
14 changes: 6 additions & 8 deletions test/mocha/when_some.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,14 @@ function isSubset(subset, superset) {
return true;
}

var RangeError = when.RangeError;

describe("when.some-test", function () {

specify("should resolve empty input", function(done) {
when.some([], 1).then(
function(result) {
assert.deepEqual(result, []);
done();
},
fail
)
specify("should reject empty input", function(done) {
when.some([], 1).caught(RangeError, function() {
done();
});
});

specify("should resolve values array", function(done) {
Expand Down

0 comments on commit 520d344

Please sign in to comment.