Commit a6b294f3 authored by Romain Courteaud's avatar Romain Courteaud

Promise: cancelling a promise created with .then stop parent propagation

parent a61853dc
...@@ -49,6 +49,7 @@ var Promise = function(resolver, canceller) { ...@@ -49,6 +49,7 @@ var Promise = function(resolver, canceller) {
// For now, simply reject the promise and does not propagate the cancel // For now, simply reject the promise and does not propagate the cancel
// to parent or children // to parent or children
if (resolved) { return; } if (resolved) { return; }
promise.isCancelled = true;
if (canceller !== undefined) { if (canceller !== undefined) {
try { try {
canceller(); canceller();
...@@ -80,6 +81,7 @@ var invokeCallback = function(type, promise, callback, event) { ...@@ -80,6 +81,7 @@ var invokeCallback = function(type, promise, callback, event) {
if (promise.isFulfilled) { return; } if (promise.isFulfilled) { return; }
if (promise.isRejected) { return; } if (promise.isRejected) { return; }
if (promise.isCancelled) { return; }
if (hasCallback) { if (hasCallback) {
try { try {
...@@ -110,6 +112,7 @@ var invokeCallback = function(type, promise, callback, event) { ...@@ -110,6 +112,7 @@ var invokeCallback = function(type, promise, callback, event) {
Promise.prototype = { Promise.prototype = {
constructor: Promise, constructor: Promise,
isCancelled: undefined,
isRejected: undefined, isRejected: undefined,
isFulfilled: undefined, isFulfilled: undefined,
rejectedReason: undefined, rejectedReason: undefined,
......
...@@ -1513,6 +1513,7 @@ describe("RSVP extensions", function() { ...@@ -1513,6 +1513,7 @@ describe("RSVP extensions", function() {
wrapped.cancel(); wrapped.cancel();
}); });
}); });
}); });
}); });
...@@ -1734,14 +1735,16 @@ describe("`cancel` on promise created by then", function () { ...@@ -1734,14 +1735,16 @@ describe("`cancel` on promise created by then", function () {
}); });
it('should stop parent fulFillment propagation', function (done) { it('should stop parent fulFillment propagation', function (done) {
var promise = new RSVP.resolve(1), var resolve_count = 0,
promise2 = promise.then(); promise = new RSVP.resolve(1),
promise2 = promise.then(function () {resolve_count += 1;});
promise2.cancel("Foo"); promise2.cancel("Foo");
setTimeout(function() { setTimeout(function() {
assert.equal(promise2.isRejected, true); assert.equal(promise2.isRejected, true);
assert.equal(promise2.isFulfilled, undefined); assert.equal(promise2.isFulfilled, undefined);
assert(promise2.rejectedReason instanceof RSVP.CancellationError); assert(promise2.rejectedReason instanceof RSVP.CancellationError);
assert.equal(resolve_count, 0);
done(); done();
}, 20); }, 20);
}); });
...@@ -1756,12 +1759,12 @@ describe("`cancel` on promise created by then", function () { ...@@ -1756,12 +1759,12 @@ describe("`cancel` on promise created by then", function () {
assert.equal(promise2.isRejected, true); assert.equal(promise2.isRejected, true);
assert.equal(promise2.isFulfilled, undefined); assert.equal(promise2.isFulfilled, undefined);
assert(promise2.rejectedReason instanceof RSVP.CancellationError); assert(promise2.rejectedReason instanceof RSVP.CancellationError);
assert.equal(reject_count, 1); assert.equal(reject_count, 0);
done(); done();
}, 20); }, 20);
}); });
it('should not be callable twice', function (done) { it('should be callable twice', function (done) {
var reject_count = 0, var reject_count = 0,
promise = new RSVP.reject(1), promise = new RSVP.reject(1),
promise2 = promise.fail(function () {reject_count += 1;}); promise2 = promise.fail(function () {reject_count += 1;});
...@@ -1772,7 +1775,7 @@ describe("`cancel` on promise created by then", function () { ...@@ -1772,7 +1775,7 @@ describe("`cancel` on promise created by then", function () {
assert.equal(promise2.isRejected, true); assert.equal(promise2.isRejected, true);
assert.equal(promise2.isFulfilled, undefined); assert.equal(promise2.isFulfilled, undefined);
assert(promise2.rejectedReason instanceof RSVP.CancellationError); assert(promise2.rejectedReason instanceof RSVP.CancellationError);
assert.equal(reject_count, 1); assert.equal(reject_count, 0);
done(); done();
}, 20); }, 20);
}); });
......
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