Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate fulfilled value as it is #221

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions q.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
27 changes: 27 additions & 0 deletions spec/q-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand Down