Thenable objects that arise along the
then()
chain are always resolved
— theonFulfilled
handler never receives a thenable object, and any
thenable returned by either handler are always resolved before being
passed to the next handler. This is because when constructing the new
promise, theresolve
andreject
functions passed by theexecutor
are
saved, and when the current promise settles, the respective function
will be called with the fulfillment value or rejection reason. The
resolving logic comes from the resolver function passed by the
> Promise()
constructor.
According to my understanding, promises are not always resolved but can also be rejected.
What does this documentation mean, is there a mistake?
2
Answers
I think your confusion is regarding the use of the word "resolved". A "resolved" promise or a thenable doesn’t always means that it has fulfilled.
A "resolved" promise could be settled (rejected or fulfilled) or locked-in on another promise and waits for that other promise to settle before settling itself with the same fulfilment or rejection value. This locking of one promise on to another promise is also referred to as one promise resolving to another promise.
From MDN – Promise #Description
(emphasis mine)
This is pretty confusing indeed. For one, in the promise terminology the "resolved" state does not mean the same as "fulfilled" – a resolved promise can have been resolved with a value so that it eventually rejects. However, I don’t think that is what the sentence
even refers to. "To be resolved" does not mean the state of the thenable object, which is not known in general. Promises do have states that are well-defined and the specification describes how promise objects transition between these states. Thenable objects on the other hand are not characterised by anything apart from their
.then()
method, which is called with two callbacks and may do anything it wants. So unless it is a promise, a thenable object does not have a state that distinguishes pending, fulfilled and rejected, and we don’t talk about it "being resolved" either.So what else does the sentence mean then? I don’t know the intention of the author but I can make a guess:
resolve()
d with the thenable, not the thenable object. But still, callingPromise.resolve(thenable)
might be simplified to "resolving the thenable".resolve()
ing aPromise
with a value, but we are resolving a promise to a value. This might take multiple steps, but in the end we are getting the eventual result value (or rejection) – not as a return value from a function call, rather as a callback argument in continuation-passing style, but still. In this same sense, we can resolve a thenable object to its result value, and that resolved value is what the chained promise gets settled with.