Commit 361f17d8 authored by Tristan Cavelier's avatar Tristan Cavelier

Promise.then cancel on non cancellable thenable fixed

parent a79070b4
...@@ -201,7 +201,9 @@ function handleThenable(promise, value) { ...@@ -201,7 +201,9 @@ function handleThenable(promise, value) {
if (isFunction(then)) { if (isFunction(then)) {
promise.on('promise:cancelled', function(event) { promise.on('promise:cancelled', function(event) {
if (isFunction(value.cancel)) {
value.cancel(); value.cancel();
}
}); });
then.call(value, function(val) { then.call(value, function(val) {
if (resolved) { return true; } if (resolved) { return true; }
......
...@@ -1591,6 +1591,88 @@ describe("`cancel` on promise created by then", function () { ...@@ -1591,6 +1591,88 @@ describe("`cancel` on promise created by then", function () {
}); });
describe("`cancel` on thenable created by then", function () {
it('should cancel the pending fulfilled promise', function (done) {
var cancel_called = false,
promise = new RSVP.Promise(function (resolve) {resolve();}),
promise2 = promise.then(function () {
return {"then": function() {}, "cancel": function () {
cancel_called = true;
}};
});
setTimeout(function() {
promise2.cancel("Foo");
setTimeout(function() {
assert.equal(cancel_called, true);
assert.equal(promise2.isFulfilled, undefined);
assert.equal(promise2.isRejected, true);
assert(promise2.rejectedReason instanceof RSVP.CancellationError);
done();
}, 20);
}, 20);
});
it('should cancel the pending rejected promise', function (done) {
var cancel_called = false,
promise = new RSVP.Promise(function (resolve, reject) {reject();}),
promise2 = promise.then(undefined, function () {
return {"then": function() {}, "cancel": function () {
cancel_called = true;
}};
});
setTimeout(function() {
promise2.cancel("Foo");
setTimeout(function() {
assert.equal(cancel_called, true);
assert.equal(promise2.isFulfilled, undefined);
assert.equal(promise2.isRejected, true);
assert(promise2.rejectedReason instanceof RSVP.CancellationError);
done();
}, 20);
}, 20);
});
});
describe("`cancel` on non cancellable thenable created by then", function () {
it('should cancel the pending fulfilled promise', function (done) {
var cancel_called = false,
promise = new RSVP.Promise(function (resolve) {resolve();}),
promise2 = promise.then(function () {
return {"then": function() {}};
});
setTimeout(function() {
promise2.cancel("Foo");
setTimeout(function() {
assert.equal(promise2.isFulfilled, undefined);
assert.equal(promise2.isRejected, true);
assert(promise2.rejectedReason instanceof RSVP.CancellationError);
done();
}, 20);
}, 20);
});
it('should cancel the pending rejected promise', function (done) {
var cancel_called = false,
promise = new RSVP.Promise(function (resolve, reject) {reject();}),
promise2 = promise.then(undefined, function () {
return {"then": function() {}};
});
setTimeout(function() {
promise2.cancel("Foo");
setTimeout(function() {
assert.equal(promise2.isFulfilled, undefined);
assert.equal(promise2.isRejected, true);
assert(promise2.rejectedReason instanceof RSVP.CancellationError);
done();
}, 20);
}, 20);
});
});
describe("`always` on a promise", function () { describe("`always` on a promise", function () {
it('should be a function', function () { it('should be a function', function () {
var promise = new RSVP.Promise(function (done, fail) {}); var promise = new RSVP.Promise(function (done, fail) {});
......
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