From 69e1082c95daa0b8da99844c1db0ecd910b15db7 Mon Sep 17 00:00:00 2001 From: Stefan Penner Date: Wed, 3 Feb 2016 22:07:44 -0800 Subject: [PATCH] [BUGFIX] finally kill self-fulfillment --- lib/rsvp/-internal.js | 18 +++++++----------- test/extension-test.js | 13 +++++++++---- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/rsvp/-internal.js b/lib/rsvp/-internal.js index 00cee0b9..881687cc 100644 --- a/lib/rsvp/-internal.js +++ b/lib/rsvp/-internal.js @@ -12,6 +12,10 @@ function withOwnPromise() { return new TypeError('A promises callback cannot return that same promise.'); } +function cycleDetected() { + return new TypeError('Chaining cycle detected for promise'); +} + function noop() {} var PENDING = void 0; @@ -43,11 +47,7 @@ function handleForeignThenable(promise, thenable, then) { var error = tryThen(then, thenable, function(value) { if (sealed) { return; } sealed = true; - if (thenable !== value) { - resolve(promise, value, undefined); - } else { - fulfill(promise, value); - } + resolve(promise, value); }, function(reason) { if (sealed) { return; } sealed = true; @@ -70,11 +70,7 @@ function handleOwnThenable(promise, thenable) { reject(promise, thenable._result); } else { subscribe(thenable, undefined, function(value) { - if (thenable !== value) { - resolve(promise, value, undefined); - } else { - fulfill(promise, value); - } + resolve(promise, value); }, function(reason) { reject(promise, reason); }); @@ -101,7 +97,7 @@ function handleMaybeThenable(promise, maybeThenable, then) { function resolve(promise, value) { if (promise === value) { - fulfill(promise, value); + reject(promise, cycleDetected()); } else if (objectOrFunction(value)) { handleMaybeThenable(promise, value, getThen(value)); } else { diff --git a/test/extension-test.js b/test/extension-test.js index 4fb376f4..bda8794b 100644 --- a/test/extension-test.js +++ b/test/extension-test.js @@ -127,11 +127,12 @@ describe("RSVP extensions", function() { }, 1); return promiseB; + }).catch(function(c) { + assert.equal(c.message, 'Chaining cycle detected for promise'); + done(); }); - promiseB.then(function(c){ - done(); - }) + promiseB.catch(done); aDefer.resolve(promiseA); }); @@ -342,7 +343,11 @@ describe("RSVP extensions", function() { }); promise.then(function(value) { - assert.equal(value, originalPromise); + done("should not be fullfilled"); + }); + + promise.catch(function(reason) { + assert.equal(reason.message, 'Chaining cycle detected for promise'); done(); }); });