This article has solved my long-term confusion, and I hope it can solve the confusion of my friends. Thank you.

What are the three states of Promise?

  1. pending
  2. fulfilled
  3. rejected

Now let’s look at three states

1. Pending state Promises

const promise1 = new Promise((resolve,reject) = >{})console.log(promise1);
Copy the code

This is a big Promise

const promise1 = new Promise((resolve, reject) = > {
    setTimeout(() = > {
        resolve()
    })
})
console.log(promise1);
Copy the code

3. The rejected state Promise

const promise = new Promise((resolve, reject) = > {
        setTimeout(() = >{ reject(); })});console.log(promise);
Copy the code

The process of changing from pending state to depressing state

  • The state of resolve() changes from pending to depressing
const promise = new Promise((resolve, reject) = > {
        setTimeout(() = > {
            console.log(State of a Promise before resolve:,promise);
            resolve();
            console.log('State of a Promise after resolve:',promise); })});Copy the code

The process of changing from the Pending state to the Rejected state

  • Reject () changed its status from Pending to Rejected
const promise = new Promise((resolve, reject) = > {
        setTimeout(() = > {
            console.log("Promise state: reject,promise);
            reject();
            console.log("State of Promise: reject:",promise); })});Copy the code

Here are some possible questions about the Promise state

Question 1: Is the resolved state and the regrettable state the same thing?

A: A state of resoved refers to a state which is completed and cannot be changed again. It is not a real state, but a pending state, which is a pity and rejected. So resolved state may be fulfilled or Rejected.

Question 2: The state displayed by Chrome is pending, but the button is fulfilled. Which state is the real state?

In this case, we can still call it a pending state.

reference

What is the relationship between resolved and fulfilled in promises, or is it just a matter of name?

The state and phenomenon of Promise