DevilRoshan / Question

前端问题解决
0 stars 0 forks source link

[Daily]以下代码执行顺序 #86

Closed DevilRoshan closed 4 years ago

DevilRoshan commented 4 years ago
// 以下代码执行顺序
new Promise((resolve, reject) => {
  console.log("A");
  setTimeout(() => {
    console.log("B");
  }, 0)
  console.log("C");
  resolve();
  console.log("D");
}).then(() => {
  console.log("E");
  new Promise((resolve, reject) => {
    console.log("F");
    resolve();
    console.log("G");
  }).then(() => {
    setTimeout(() => {
      console.log("H");
    }, 0)
    console.log("I");
  }).then(() => {
    console.log("J");
  })
}).then(() => {
  console.log("K")
})

setTimeout(() => {
  console.log("L")
})

new Promise((resolve, reject) => {
  console.log("M");
  resolve();
}).then(() => {
  setTimeout(() => {
    new Promise((resolve, reject) => {
      console.log("N");
      resolve();
    }).then(() => {
      setTimeout(() => {
        console.log("O");
      }, 0)
    }).then(() => {
      console.log("P");
    })
  })
})

console.log("Q")
DevilRoshan commented 4 years ago

https://github.com/DevilRoshan/Question/blob/master/20200722-js-event-loop-2.md