Commit fdaaddad authored by Romain Courteaud's avatar Romain Courteaud

Cancelling RSVP.resolve should also cancel thenable.

parent f11787e6
...@@ -3,7 +3,17 @@ var Promise = require("./promise").Promise; ...@@ -3,7 +3,17 @@ var Promise = require("./promise").Promise;
function resolve(thenable) { function resolve(thenable) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
resolve(thenable); if (typeof thenable === "object" && thenable !== null) {
var then = thenable.then;
if ((then !== undefined) && (typeof then === "function")) {
return then.apply(thenable, [resolve, reject]);
}
}
return resolve(thenable);
}, function () {
if ((thenable !== undefined) && (thenable.cancel !== undefined)) {
thenable.cancel();
}
}); });
} }
......
...@@ -828,7 +828,17 @@ define("rsvp/resolve", ...@@ -828,7 +828,17 @@ define("rsvp/resolve",
function resolve(thenable) { function resolve(thenable) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
resolve(thenable); if (typeof thenable === "object" && thenable !== null) {
var then = thenable.then;
if ((then !== undefined) && (typeof then === "function")) {
return then.apply(thenable, [resolve, reject]);
}
}
return resolve(thenable);
}, function () {
if ((thenable !== undefined) && (thenable.cancel !== undefined)) {
thenable.cancel();
}
}); });
} }
......
...@@ -865,7 +865,17 @@ define("rsvp/resolve", ...@@ -865,7 +865,17 @@ define("rsvp/resolve",
function resolve(thenable) { function resolve(thenable) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
resolve(thenable); if (typeof thenable === "object" && thenable !== null) {
var then = thenable.then;
if ((then !== undefined) && (typeof then === "function")) {
return then.apply(thenable, [resolve, reject]);
}
}
return resolve(thenable);
}, function () {
if ((thenable !== undefined) && (thenable.cancel !== undefined)) {
thenable.cancel();
}
}); });
} }
......
This diff is collapsed.
...@@ -2,7 +2,17 @@ import { Promise } from "./promise"; ...@@ -2,7 +2,17 @@ import { Promise } from "./promise";
function resolve(thenable) { function resolve(thenable) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
resolve(thenable); if (typeof thenable === "object" && thenable !== null) {
var then = thenable.then;
if ((then !== undefined) && (typeof then === "function")) {
return then.apply(thenable, [resolve, reject]);
}
}
return resolve(thenable);
}, function () {
if ((thenable !== undefined) && (thenable.cancel !== undefined)) {
thenable.cancel();
}
}); });
} }
......
...@@ -1335,9 +1335,10 @@ describe("RSVP extensions", function() { ...@@ -1335,9 +1335,10 @@ describe("RSVP extensions", function() {
wrapped = RSVP.resolve(thenable); wrapped = RSVP.resolve(thenable);
assert(accessCount === 1); setTimeout(function() {
assert(accessCount === 1);
done(); done();
}, 20);
}); });
specify("2.2 If retrieving the property x.then results in a thrown exception e, reject promise with e as the reason.", function(done){ specify("2.2 If retrieving the property x.then results in a thrown exception e, reject promise with e as the reason.", function(done){
...@@ -1532,6 +1533,23 @@ describe("RSVP extensions", function() { ...@@ -1532,6 +1533,23 @@ describe("RSVP extensions", function() {
assert(callCount === 0, 'expected async, was sync'); assert(callCount === 0, 'expected async, was sync');
}); });
}); });
describe("4. If x is a promise, ", function(){
specify("cancel will also cancel x.", function(done){
var thenable, wrapped;
thenable = RSVP.delay();
wrapped = RSVP.resolve(thenable);
thenable.fail(function(error){
assert(error instanceof RSVP.CancellationError, 'expected CancellationError');
done();
});
wrapped.cancel();
});
});
}); });
}); });
......
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