diff --git a/q.js b/q.js index 8af16310..1af4950d 100644 --- a/q.js +++ b/q.js @@ -784,7 +784,7 @@ function coerce(promise) { var deferred = defer(); nextTick(function () { try { - promise.then(deferred.resolve, deferred.reject, deferred.notify); + promise.then(deferred.fulfill, deferred.reject, deferred.notify); } catch (exception) { deferred.reject(exception); } @@ -1310,7 +1310,7 @@ function timeout(promise, ms) { when(promise, function (value) { clearTimeout(timeoutId); - deferred.resolve(value); + deferred.fulfill(value); }, function (exception) { clearTimeout(timeoutId); deferred.reject(exception); diff --git a/spec/q-spec.js b/spec/q-spec.js index e7ace4b9..44042a09 100644 --- a/spec/q-spec.js +++ b/spec/q-spec.js @@ -33,6 +33,21 @@ describe("Q function", function () { expect(Q(r)).toBe(r); expect(Q(p)).toBe(p); }); + + it("should coerce thenables which resoved value is also thenable", function () { + var fulfilledThenable = { + then: function (callback) { + callback(returnedValue); + // Avoid inifinite resolving in case of bad implementations. + returnedValue = {}; + } + }; + var returnedValue = fulfilledThenable; + + return Q(fulfilledThenable).then(function (value) { + expect(value).toBe(fulfilledThenable); + }); + }); }); describe("defer and when", function () { @@ -1499,6 +1514,18 @@ describe("timeout", function () { } ); }); + + it("should propagate fulfilled value as it is", function () { + var fakeThenable = { + then: function () { + throw new Error("not a promise"); + } + }; + var toResolve = Q.defer(); + var promise = toResolve.promise.timeout(10); + toResolve.resolve(Q.fulfill(fakeThenable)); + return promise; + }); }); describe("thenResolve", function () {