Any object or function that meets the promise specification can become A promise, promise A plus

The term

Promise: A Promise is an object or function that has then methods and behaves in accordance with this specification.

Thenable: is an object or function that defines the THEN method;

Value: any valid javaScript value (including undefine, thenable, and Promise)

Exception: A value thrown using a throw statement

Reason: indicates a reason for rejecting a promise

requirements

The state of the promise

The current state of a Promise must be one of the following three states: Pending, Fulfilled and Rejected.

  • In the wait state, a promise must meet the following conditions: It can be changed

“Completed” or “Rejected”

  • When a promise is completed, the following conditions must be met: 1

Move to any other state 2. Must have an immutable value

  • If a promise has been rejected, the following conditions must be met: 1

2. There must be an immutable cause

There must be a then method

A promise must provide a THEN method to access its current value and reason.

The promise’s then method accepts two parameters: Promise. Then (onFulfilled, onRejected), both of which are optional parameters, and they are functions. If onFulfilled or onRejected is not a function, you need to ignore them.

To solve the process

If x is Object or function (uncommon)

  • Try executing x. teng first
  • If an error e is thrown when taking the value x. teng, reject the promise based on e
  • If then is a function, x is called as the function’s scope this. Pass two callback functions as arguments, the first called resolvePromise and the second called rejectPromise:
  • If then is not a function, change the promise to the completed state, taking x as an argument

If x is not an object or function, change the promise to a completed state with x as an argument (important and common)

conclusion

  1. Catch has a throw, a return on a rejected promise, and that’s the only two cases where catch is followed by catch, otherwise catch is followed by then
  2. The current state of a Promise must be one of the following three states: Pending, Fulfilled and Rejected (which is a pity) and the state is irreversible
  3. Then must return a promise