skip to Main Content

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then#description

Thenable objects that arise along the then() chain are always resolved
— the onFulfilled 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, the resolve and reject functions passed by the executor 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


  1. 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

    You will also hear the term resolved used with promises — this means that the promise is settled or "locked-in" to match the eventual state of another promise, and further resolving or rejecting it has no effect. …. Colloquially, "resolved" promises are often equivalent to "fulfilled" promises, but as illustrated in "States and fates", resolved promises can be pending or rejected as well

    (emphasis mine)

    Login or Signup to reply.
  2. 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

    Thenable objects that arise along the then() chain are always resolved

    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:

    • it might mean that any thenable objects arising along the chain are used as the argument to resolve a promise. This is a bit sloppy, as it’s the promise which is being resolve()d with the thenable, not the thenable object. But still, calling Promise.resolve(thenable) might be simplified to "resolving the thenable".
    • it might mean that the result is extracted. Here the verb "resolve" is not used in its JavaScript-specific meaning, but rather in the general sense – like in "solving an equation" or "resolving a domain name". We are not only resolve()ing a Promise 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.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search