Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

异步编程: Promise 题目 2 - 3 - 1 #119

Open
leslie1943 opened this issue Jan 4, 2021 · 0 comments
Open

异步编程: Promise 题目 2 - 3 - 1 #119

leslie1943 opened this issue Jan 4, 2021 · 0 comments

Comments

@leslie1943
Copy link
Owner

Promise 题目 2 - 3 - 1

setTimeout(() => {
  console.log('timer1')

  setTimeout(() => {
    console.log('timer3')
  }, 0)
}, 0)
setTimeout(() => {
  console.log('timer2')
}, 0)

console.log('start')

// start, timer1,timer2,timer3
setTimeout(() => {
  console.log('timer1')

  Promise.resolve().then(() => {
    console.log('promise')
  })
}, 0)

setTimeout(() => {
  console.log('timer2')
}, 0)

console.log('start')

// start, timer1,promise,timer2

过程分析

  • 这两个例子, 看着好像只是把第一个定时器中的内容换了一下而已.
    一个是为定时器 timer3, 一个是为 Promise.then
  • 但是如果是定时器 timer3 的话, 它会在 timer2 后执行, 而 Promise.then 却是在 timer2 之前执行.
  • 你可以这样理解, Promise.then 是微任务, 它会被加入到本轮中的微任务列表, 而定时器 timer3 是宏任务, 它会被加入到下一轮的宏任务中.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant