0

Here is a quote about Promise.then() from MDN.

Return value

Once a Promise is fulfilled or rejected, the respective handler function (onFulfilled or onRejected) will be called asynchronously (scheduled in the current thread loop). The behavior of the handler function follows a specific set of rules. If a handler function:

  • returns a value, the promise returned by then gets resolved with the returned value as its value.
  • doesn't return anything, the promise returned by then gets resolved with an undefined value.
  • throws an error, the promise returned by then gets rejected with the thrown error as its value.
  • returns an already fulfilled promise, the promise returned by then gets fulfilled with that promise's value as its value.
  • returns an already rejected promise, the promise returned by then gets rejected with that promise's value as its value.
  • returns another pending promise object, the resolution/rejection of the promise returned by then will be subsequent to the resolution/rejection of the promise returned by the handler. Also, the resolved value of the promise returned by then will be the same as the resolved value of the promise returned by the handler.

My question is why the state of promise returned by .then is resolved rather than fulfilled in second point?

It seems only have one state fulfilled. Why not use the word fulfilled like the fourth point?

In first point and second point, both states of promise return by .then is 'resolved'.

About first point, in most cases, the state is fulfilled . And if the callback return a thenable, the state of promise may be 'rejected':

let thenableObj = {
    then(thenableRes, thenableRej) {
        thenableRej('thenable reject')
    }
}   

let p = Promise.resolve('foo')

p.then(() => {
    return thenableObj
}) // Promise{<rejecter>: 'thenable reject'}

So, I can accept the word resolved. But, in the second point, I can't find any example to prove the word resolved is more precise than fulfilled.

More specifically, can you give some examples to prove:

  1. the state of the promise returned by .then will be rejected when the callback in .then don't return anything.
  2. the state of promise returned by .then will be "locked in" to match the state of another promise when the callback in .then don't return anything.

if neither works, why don't we use the word fulfilled like fourth point than resolved

youfu
  • 11
  • 2
  • 1
    Probably just sloppy writing. If you make the [proper distinction](https://stackoverflow.com/a/29269515/1048572) between [resolving and fulfilling](https://stackoverflow.com/a/41910860/1048572), i.e. know what "resolve" really means, then you can (and should) shorten that list of 6 cases to simply the first (*returns a value*) and third (*throws a value*); those two are sufficient to describe the whole behaviour – Bergi May 12 '22 at 12:53
  • "*… when the callback in .then don't return anything*" - this is not really possible. A function always returns something - unless it diverges (`while(true);`) or throws. – Bergi May 12 '22 at 12:56

0 Answers0