How to understand the asynchronous characteristics of running [[Resolve]] (promise, x) in Promise

[[Resolve]] (promise, x)

if x is Promise, make promise accept the state of x. Note 4:
if x is in waiting state, promise shall remain in waiting state until x is executed or rejected
if x is in execution state, execute promise
if x is in reject state, reject promise
if x is not an object or function, execute promise

with x as parameter.

the above is excerpted from the Promise/A+ specification

if you follow the description in the specification, then when x = Promise.resolve (10), the behavior of the two should be the same. He will take the value of promise in the execution state and do a resolve. This whole process should be similar to grammatical sugar? But the truth seems to be a little biased

Let"s look at a piece of code

new Promise((resolve, reject) => {
  let a = new Promise(resolve => {
    resolve(10)
  })
  let b = new Promise(resolve => {
    let c = Promise.resolve(10)
    resolve(c)
  })
  console.log(a)
  console.log(b)
  Promise.resolve(0)
    .then(() => {
      console.log("micro1")
      console.log(b)
    })
    .then(() => {
      console.log("micro2")
      console.log(b)
    })
  setTimeout(() => {
    console.log("marco")
    console.log(b)
  })
})

this is the output

let b = new Promise(resolve => {
  Promise.resolve(10).then(res => {
    resolve(res)
  })
})
Jun.09,2022

you may be misled by console.log . Although the print shows that Promise is already fulfill, its onFulfilled is bound to start after the platform code has been executed (see note at the bottom of the specification).

so resolve (c) in b will not be processed until the second round of cleaning the Promise queue.

and the micro1 added in the second round will be added to the new queue in front of the stack, so it will be fulfilled before b .

micro2 has reached the third round, when b has been fulfilled.

finally, I will help you list the whole easy to understand.

A queue was added to the stack after the first platform code execution

- micro2
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b31ad2-2aaf9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b31ad2-2aaf9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?