You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(注:
Task queues are sets, not queues, because step one of the event loop processing
model grabs the first runnable task from the chosen queue, instead of dequeuing the first task.)
whatwg-event-loop规范
microtask-queue
macrotasks(宏任务): setTimeout ,setInterval, setImmediate,requestAnimationFram,I/O ,UI渲染
microtasks(微任务): Promise, process.nextTick, Object.observe, MutationObserver
macrotask whatwg 规范里称为 task queues, 一个 event-loop 中可能有一个或者多个 task queues ,
有一个 microtask 队列(不清楚是否只有一个), 微任务队列不是任务队列
举个🌰:
更复杂一些的🌰:
总结:
1.1 遇到微任务,就加到微任务队列中.
1.2 遇到宏任务,就加到宏任务sets 中.
2.1 当此时执行的微任务里面又 创建了微任务或者宏任务的话
2.2 将宏任务加到宏任务队列,将微任务加到微任务队尾
2.3 当微任务队列全部为空时,微任务执行完毕。
(注:
Task queues are sets, not queues, because step one of the event loop processing
model grabs the first runnable task from the chosen queue, instead of dequeuing the first task.)
PS: event-loop 还是比较复杂的,规范看起来有难度(英文加涉及到的概念太多)目前没看完,后续慢慢学习。
因为 Node 里面的event-loop 实现机制不太一样,上面的解释对于Node里的event-loop不适合
参考:
从浏览器多进程到JS单线程,JS运行机制最全面的一次梳理
difference-between-microtask-and-macrotask-within-an-event-loop-context
理解事件循环二(macrotask和microtask)
The text was updated successfully, but these errors were encountered: