Skip to content

Instantly share code, notes, and snippets.

@Debbl
Created March 7, 2022 12:16
Show Gist options
  • Save Debbl/c0baf8caede48b445d83fdf104da799f to your computer and use it in GitHub Desktop.
Save Debbl/c0baf8caede48b445d83fdf104da799f to your computer and use it in GitHub Desktop.

宏任务和微任务

宏任务队列(macrotask queue)

  • ajax
  • setTimeout
  • setInterval
  • DOM 监听
  • UI Rendering 等

微任务队列 (microtask queue)

  • Promise 的 then 回调
  • Mutation Observer API
  • queueMicrotask() 等

优先级

  1. main script中的代码优先执行(编写的顶层script代码)
  2. 在执行任何一个宏任务之前(不是队列,是一个宏任务),都会先查看微任务队列中是否有任务需要执行
    • 也就是宏任务执行之前,必须保证微任务队列是空的
    • 如果不为空,那么就优先执行微任务队列中的任务(回调);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment