diff --git a/lib/rsvp/promise/all.js b/lib/rsvp/promise/all.js index 81e7f384..ec5380cd 100644 --- a/lib/rsvp/promise/all.js +++ b/lib/rsvp/promise/all.js @@ -46,14 +46,15 @@ import { isArray, isNonThenable } from "../utils"; fulfilled, or rejected if any of them become rejected. */ export default function all(entries, label) { - if (!isArray(entries)) { - throw new TypeError('You must pass an array to all.'); - } /*jshint validthis:true */ var Constructor = this; return new Constructor(function(resolve, reject) { + if (!isArray(entries)) { + throw new TypeError('You must pass an array to all.'); + } + var remaining = entries.length; var results = new Array(remaining); var entry, pending = true; diff --git a/lib/rsvp/promise/race.js b/lib/rsvp/promise/race.js index 40665b0c..09460286 100644 --- a/lib/rsvp/promise/race.js +++ b/lib/rsvp/promise/race.js @@ -65,14 +65,14 @@ import { isArray, isFunction, isNonThenable } from "../utils"; was rejected with. */ export default function race(entries, label) { - if (!isArray(entries)) { - throw new TypeError('You must pass an array to race.'); - } - /*jshint validthis:true */ var Constructor = this, entry; return new Constructor(function(resolve, reject) { + if (!isArray(entries)) { + throw new TypeError('You must pass an array to race.'); + } + var pending = true; function onFulfillment(value) { if (pending) { pending = false; resolve(value); } } diff --git a/test/index.html b/test/index.html index b0875e20..2acb9e76 100644 --- a/test/index.html +++ b/test/index.html @@ -6,7 +6,7 @@
- + diff --git a/test/tests/extension_test.js b/test/tests/extension_test.js index a45ea5f7..c5b869f0 100644 --- a/test/tests/extension_test.js +++ b/test/tests/extension_test.js @@ -598,17 +598,15 @@ describe("RSVP extensions", function() { }); it('throws when not passed an array', function() { - assert.throws(function () { - var all = all(); - }, TypeError); - - assert.throws(function () { - var all = all(''); - }, TypeError); + var nothing = assertRejection(all()); + var string = assertRejection(all('')); + var object = assertRejection(all({})); - assert.throws(function () { - var all = all({}); - }, TypeError); + RSVP.Promise.all([ + nothing, + string, + object + ]).then(function(){ done(); }); }); specify('fulfilled only after all of the other promises are fulfilled', function(done) { @@ -809,23 +807,29 @@ describe("RSVP extensions", function() { }); }); + function assertRejection(promise) { + return promise.then(function(){ + assert(false, 'expected rejection, but got fulfillment'); + }, function(reason){ + assert(reason instanceof Error); + }); + } + function testRace(race) { it("should exist", function() { assert(race); }); - it("throws when not passed an array", function() { - assert.throws(function () { - race(); - }, TypeError); - - assert.throws(function () { - var race = race(''); - }, TypeError); + it("throws when not passed an array", function(done) { + var nothing = assertRejection(race()); + var string = assertRejection(race('')); + var object = assertRejection(race({})); - assert.throws(function () { - race({}); - }, TypeError); + RSVP.Promise.all([ + nothing, + string, + object + ]).then(function(){ done(); }); }); specify('fulfilled after one of the other promises are fulfilled', function(done) {