Skip to content

Commit

Permalink
Merge pull request #189 from tildeio/all_race_should_reject_not_throw
Browse files Browse the repository at this point in the history
All race should reject not throw
  • Loading branch information
stefanpenner committed Jan 4, 2014
2 parents f9fe829 + 24dc615 commit 60aafad
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
7 changes: 4 additions & 3 deletions lib/rsvp/promise/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions lib/rsvp/promise/race.js
Original file line number Diff line number Diff line change
Expand Up @@ -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); } }
Expand Down
2 changes: 1 addition & 1 deletion test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</head>
<body>
<div id="mocha"></div>
<script src="../dist/rsvp-2.0.4.js"></script>
<script src="../dist/rsvp-3.0.1.js"></script>
<script src="vendor/assert.js"></script>
<script src="vendor/mocha.js"></script>
<script>mocha.setup({ ui: 'bdd', timeout: 200 }); mocha.globals(['setTimeout']);</script>
Expand Down
46 changes: 25 additions & 21 deletions test/tests/extension_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 60aafad

Please sign in to comment.