Commit 60f83a3b authored by Stefan Penner's avatar Stefan Penner

[Fix] promises could be changed after reject/resolve

parent 71a9de8d
...@@ -372,7 +372,8 @@ define("rsvp/promise", ...@@ -372,7 +372,8 @@ define("rsvp/promise",
var noop = function() {}; var noop = function() {};
var Promise = function(resolver) { var Promise = function(resolver) {
var promise = this; var promise = this,
resolved = false;
if (typeof resolver !== 'function') { if (typeof resolver !== 'function') {
throw new TypeError('You must pass a resolver function as the sole argument to the promise constructor'); throw new TypeError('You must pass a resolver function as the sole argument to the promise constructor');
...@@ -383,15 +384,15 @@ define("rsvp/promise", ...@@ -383,15 +384,15 @@ define("rsvp/promise",
} }
var resolvePromise = function(value) { var resolvePromise = function(value) {
if (resolved) { return; }
resolved = true;
resolve(promise, value); resolve(promise, value);
resolvePromise = noop;
rejectPromise = noop;
}; };
var rejectPromise = function(value) { var rejectPromise = function(value) {
if (resolved) { return; }
resolved = true;
reject(promise, value); reject(promise, value);
resolvePromise = noop;
rejectPromise = noop;
}; };
this.on('promise:resolved', function(event) { this.on('promise:resolved', function(event) {
......
This diff is collapsed.
...@@ -8,7 +8,8 @@ define( ...@@ -8,7 +8,8 @@ define(
var noop = function() {}; var noop = function() {};
var Promise = function(resolver) { var Promise = function(resolver) {
var promise = this; var promise = this,
resolved = false;
if (typeof resolver !== 'function') { if (typeof resolver !== 'function') {
throw new TypeError('You must pass a resolver function as the sole argument to the promise constructor'); throw new TypeError('You must pass a resolver function as the sole argument to the promise constructor');
...@@ -19,15 +20,15 @@ define( ...@@ -19,15 +20,15 @@ define(
} }
var resolvePromise = function(value) { var resolvePromise = function(value) {
if (resolved) { return; }
resolved = true;
resolve(promise, value); resolve(promise, value);
resolvePromise = noop;
rejectPromise = noop;
}; };
var rejectPromise = function(value) { var rejectPromise = function(value) {
if (resolved) { return; }
resolved = true;
reject(promise, value); reject(promise, value);
resolvePromise = noop;
rejectPromise = noop;
}; };
this.on('promise:resolved', function(event) { this.on('promise:resolved', function(event) {
......
import { config } from "rsvp/config"; import { config } from "rsvp/config";
import { EventTarget } from "rsvp/events"; import { EventTarget } from "rsvp/events";
var noop = function() {};
var Promise = function(resolver) { var Promise = function(resolver) {
var promise = this; var promise = this,
resolved = false;
if (typeof resolver !== 'function') { if (typeof resolver !== 'function') {
throw new TypeError('You must pass a resolver function as the sole argument to the promise constructor'); throw new TypeError('You must pass a resolver function as the sole argument to the promise constructor');
...@@ -15,15 +14,15 @@ var Promise = function(resolver) { ...@@ -15,15 +14,15 @@ var Promise = function(resolver) {
} }
var resolvePromise = function(value) { var resolvePromise = function(value) {
if (resolved) { return; }
resolved = true;
resolve(promise, value); resolve(promise, value);
resolvePromise = noop;
rejectPromise = noop;
}; };
var rejectPromise = function(value) { var rejectPromise = function(value) {
if (resolved) { return; }
resolved = true;
reject(promise, value); reject(promise, value);
resolvePromise = noop;
rejectPromise = noop;
}; };
this.on('promise:resolved', function(event) { this.on('promise:resolved', function(event) {
......
...@@ -705,12 +705,21 @@ describe("RSVP extensions", function() { ...@@ -705,12 +705,21 @@ describe("RSVP extensions", function() {
assert(calledResolved === 0, 'never resolved'); assert(calledResolved === 0, 'never resolved');
assert(calledRejected === 1, 'rejected only once'); assert(calledRejected === 1, 'rejected only once');
assert(error === expectedError, 'rejected promise with x'); assert(error === expectedError, 'rejected promise with x');
done();
}); });
rejector(expectedError); rejector(expectedError);
rejector(expectedError); rejector(expectedError);
resolver();
rejector('foo');
resolver('bar');
resolver('baz');
setTimeout(function(){
assert(calledRejected === 1, 'only rejected once');
assert(calledResolved === 0, 'never resolved');
done();
}, 50);
}); });
describe("2.3.4 If calling then throws an exception e", function(){ describe("2.3.4 If calling then throws an exception e", function(){
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment