We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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.resolve().then(() => { console.log('promise1') const timer2 = setTimeout(() => { console.log('timer2') }, 0) }) const timer1 = setTimeout(() => { console.log('timer1') Promise.resolve().then(() => { console.log('promise2') }) }, 0) console.log('start') // start, promise1, timer1,promise2,timer2
promise
Promise
resolve
new Promise
宏1
Promise.resolve().then
then
微1
timer1
宏2
start
(宏1)
(微1)
promise.then
promise1
timer2
宏3
promise2
微2
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Promise 题目 2 - 3 - 2
过程分析
promise
中执行定时器, 又在定时器中执行promise
Promise
是直接resolve
的, 而之前的new Promise
不一样宏1
, 从上至下执行Promise.resolve().then
这个微任务, 将then
中的内容加入第一次的微任务队列标记为微1
timer1
, 将它加入下一次宏任务的延迟列表, 标记为宏2
, 等待执行(先不管里面是什么内容)宏1
中的同步代码start
(宏1)
执行完毕, 检查第一次的微任务队列(微1)
, 发现有一个promise.then
这个微任务需要执行微1
中同步代码promise1
, 然后发现定时器timer2
, 将它加入宏2
的后面, 标记为宏3
微1
)执行完毕, 执行第二次宏任务(宏2
), 首先执行同步代码timer1
promise2
这个微任务, 将它加入此次循环的微任务队列, 标记为微2
宏2
中没有同步代码可执行了, 查找本次循环的微任务队列(微2
), 发现了promise2
, 执行它宏3
, 打印出timer2
The text was updated successfully, but these errors were encountered: