1. Let’s take a look at the code output from Promise for then:

    Promise.resolve().then(() = >{
        console.log(0);
        return Promise.resolve(4);
    }).then((res) = >{
        console.log(res)
    })
    Promise.resolve().then(() = >{
        console.log(1);
    }).then(() = >{
        console.log(2);
    }).then(() = >{
        console.log(3);
    }).then(() = >{
        console.log(5);
    }).then(() = >{
        console.log(6); }) output:0
    1
    2
    3
    4 // Find that 4 is delayed by two sequences, and that the then between multiple promises is run alternately
    5
    6
    
    / * 0 - > first of all, the browser will create a PromiseResolveThenableJob to deal with the Promise (resolved) instance (then the back of the function is to return a Promise, You can no longer return stateless promises in the body of a function, otherwise you'll be stuck in an infinite loop), which is a microtask. Into another, then the output 1 - > then, wait for the next cycle running to the micro task, also is in the implementation of the PromiseResolveThenableJob time, for instance the Promise is fulfilled (resolved) state, So it registers one of its.then() callbacks. Enter another THEN, and output 2 -> Wait until the next loop to this Promise instance, and its.then() callback executes, passing the argument to the next THEN (output res, which will be printed at the next loop), thus delaying the next THEN by two times. Output 3 -> Received parameters, output 4 -> Output 5 -> output 6 */
    Copy the code
  2. V8 engine about promise source:

    Resolve (4) return promise.resolve (4) return new Promise(resolve=>resolve(4)) If so, execute x. Chen, which is a recursive call, and the code for the recursive call will be executed in the next event loop (0,1, then 2). The code then runs through this recursion, executes resolve(4), prints 3, and the next time it runs through this code, prints 4.

  3. Resolve () is used to indicate that the state of a promise is fullfilled. Resolve () defines a stateful promise, but does not invoke it.

    When using then, note:

    • A promise calls then if the state of the promise is fullfilled
    • Only when a promise calls then will the function inside that then be pushed into the microtask;
  4. Chain calls for a single promise:

    1. When a promise executes, a Promise object in a pending state is created to execute the code in the promise body.

      • If the function body is in the fullfilled state, then code will be called and then code will be placed in the microtask queue of this event loop.
      • If the state of the function body is pending, the code following then will be queued in the promise. When the state of the promise becomes fullfilled, the code will be pushed into the event loop microtask queue for immediate execution.

      Example:

      new Promise((resolve, reject) = > {
              console.log("promise")
              resolve()
      })
      .then(() = > {	// Then generates a promise for the last. Then
              console.log("then1")
          new Promise((resolve, reject) = > {
              console.log("then1promise")
              resolve()
              })
              .then(() = > {// When executing this. Then, a promise is generated for the next THEN
              console.log("then1then1")
          })
          .then(() = > {
              console.log("then1then2")
              })
      })
      .then(() = > {
        / / this
              console.log("then2"}) Output: Promise then1 then1promise then1then1 then2 then1then2Copy the code