Commit bebc30b9 authored by Romain Courteaud's avatar Romain Courteaud

Prevent resolving a promise twice.

parent d6449937
...@@ -78,6 +78,9 @@ var invokeCallback = function(type, promise, callback, event) { ...@@ -78,6 +78,9 @@ var invokeCallback = function(type, promise, callback, event) {
var hasCallback = isFunction(callback), var hasCallback = isFunction(callback),
value, error, succeeded, failed; value, error, succeeded, failed;
if (promise.isFulfilled) { return; }
if (promise.isRejected) { return; }
if (hasCallback) { if (hasCallback) {
try { try {
value = callback(event.detail); value = callback(event.detail);
...@@ -207,6 +210,8 @@ function handleThenable(promise, value) { ...@@ -207,6 +210,8 @@ function handleThenable(promise, value) {
function fulfill(promise, value) { function fulfill(promise, value) {
config.async(function() { config.async(function() {
if (promise.isFulfilled) { return; }
if (promise.isRejected) { return; }
promise.trigger('promise:resolved', { detail: value }); promise.trigger('promise:resolved', { detail: value });
promise.isFulfilled = true; promise.isFulfilled = true;
promise.fulfillmentValue = value; promise.fulfillmentValue = value;
...@@ -215,6 +220,8 @@ function fulfill(promise, value) { ...@@ -215,6 +220,8 @@ function fulfill(promise, value) {
function reject(promise, value) { function reject(promise, value) {
config.async(function() { config.async(function() {
if (promise.isFulfilled) { return; }
if (promise.isRejected) { return; }
promise.trigger('promise:failed', { detail: value }); promise.trigger('promise:failed', { detail: value });
promise.isRejected = true; promise.isRejected = true;
promise.rejectedReason = value; promise.rejectedReason = value;
......
...@@ -1489,6 +1489,50 @@ describe("`cancel` on promise created by then", function () { ...@@ -1489,6 +1489,50 @@ describe("`cancel` on promise created by then", function () {
assert.equal(typeof promise.cancel, "function"); assert.equal(typeof promise.cancel, "function");
}); });
it('should stop parent fulFillment propagation', function (done) {
var promise = new RSVP.resolve(1),
promise2 = promise.then();
promise2.cancel("Foo");
setTimeout(function() {
assert.equal(promise2.isRejected, true);
assert.equal(promise2.isFulfilled, undefined);
assert(promise2.rejectedReason instanceof RSVP.CancellationError);
done();
}, 20);
});
it('should stop parent rejection propagation', function (done) {
var reject_count = 0,
promise = new RSVP.reject(1),
promise2 = promise.fail(function () {reject_count += 1;});
promise2.cancel("Foo");
setTimeout(function() {
assert.equal(promise2.isRejected, true);
assert.equal(promise2.isFulfilled, undefined);
assert(promise2.rejectedReason instanceof RSVP.CancellationError);
assert.equal(reject_count, 1);
done();
}, 20);
});
it('should not be callable twice', function (done) {
var reject_count = 0,
promise = new RSVP.reject(1),
promise2 = promise.fail(function () {reject_count += 1;});
promise2.cancel("Foo");
promise2.cancel("Bar");
setTimeout(function() {
assert.equal(promise2.isRejected, true);
assert.equal(promise2.isFulfilled, undefined);
assert(promise2.rejectedReason instanceof RSVP.CancellationError);
assert.equal(reject_count, 1);
done();
}, 20);
});
it('should cancel the pending fulfilled promise', function (done) { it('should cancel the pending fulfilled promise', function (done) {
var cancel_called = false, var cancel_called = false,
promise = new RSVP.Promise(function (resolve) {resolve();}), promise = new RSVP.Promise(function (resolve) {resolve();}),
...@@ -1804,13 +1848,12 @@ describe("`RSVP.Queue`", function () { ...@@ -1804,13 +1848,12 @@ describe("`RSVP.Queue`", function () {
assert(promise instanceof RSVP.Promise); assert(promise instanceof RSVP.Promise);
}); });
it('should work without `new`', function(done) { it('should work without `new`', function() {
var promise = RSVP.Queue() var promise = RSVP.Queue()
.push(function() { return 'value'; }); .push(function() { return 'value'; });
promise.then(function(value) { promise.then(function(value) {
assert.equal(value, 'value'); assert.equal(value, 'value');
done();
}); });
}); });
}); });
......
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