qiuhongbingo / blog

Writing something in Issues.
https://github.com/qiuhongbingo/blog/issues
3 stars 0 forks source link

宏任务和微任务?谁快谁慢 #11

Open qiuhongbingo opened 4 years ago

qiuhongbingo commented 4 years ago
console.log('start here') // 1

const foo = () =>
  new Promise((resolve, reject) => {
    console.log('first promise constructor') // 2

    let promise1 = new Promise((resolve, reject) => {
      console.log('second promise constructor') // 3

      setTimeout(() => {
        console.log('setTimeout here') // 7 宏任务
        resolve()
      }, 0)

      resolve('promise1') // 微任务优先
    })

    resolve('promise0') // 微任务优先

    promise1.then(arg => {
      console.log(arg) // 5
    })
  })

foo().then(arg => {
  console.log(arg) // 6
})

console.log('end here') // 4