Eventloop趕上Promise

Promise.resolve(1).then(() => {
  console.log('then1')
  Promise.resolve(2).then(() => {
    console.log('then3')
    Promise.resolve(3).then(()=> {
      console.log('then4')
      Promise.resolve(4).then(() => {
        console.log('then5')
        Promise.resolve(5).then(()=> {
          console.log('then6')
        })
      })
    }).then(() => {
      console.log('then7')
    })
  }).then(() => {
    console.log('then8')
  })
}).then(() => {
  console.log('then2')
}).then(() => {
  console.log('then9')
}).then(() => {
  console.log('then10')
})
    
複製代碼

Ready GOpromise

Promise.resolve()至關與new Promise((resolve,reject)=>{resolve()})bash

第一輪函數

  • current task: resolve(1)是當之無愧的當即執行的一個函數
  • micro task queue: [resolve(1)的第一個then]

第二輪spa

  • current task: then1執行中,當即輸出了then1以及新resolve(2)的promise
  • micro task queue: [新resolve(2)的then函數,以resolve(1)的第二個then函數]

第三輪code

  • current task: 新resolve(2)的then函數輸出then3,新resolve(3)的promise和resolve(1)的第二個then函數輸出then2
  • micro task queue: [新resolve(3)的then函數, resolve(2)的第二then函數, resolve(1)的第三個then函數]

第四輪string

  • current task: resolve(3)的then函數輸出then4,新resolve(4)的promise,resolve(2)的第二then函數輸出then8和resolve(1)的第三個then函數輸出then9
  • micro task queue: [新resolve(4)的then函數,resolve(3)的第二個then函數, resolve(1)的第三個then函數]

第五輪console

  • current task: resolve(4)的then函數輸出then5,新resolve(5)的promise,resolve(3)的第二then函數輸出then7和resolve(1)的第三個then函數輸出then10
  • micro task queue: [新resolve(5)的then函數]

第六輪class

  • current task: resolve(5)的then函數輸出then6
  • micro task queue: []
相關文章
相關標籤/搜索