02 · Event Loop & Asynchronous JavaScript
mediumcodingjavascript~8 min

Microtasks vs Macrotasks

Objective: Pick the right scheduling primitive (sync call, queueMicrotask, setTimeout) to control ordering.

`scheduleOrder` logs three phases, but they all run synchronously in the wrong order. Schedule each with the right mechanism so they run as sync → microtask → macrotask.

  • Call log('sync') synchronously.
  • Call log('microtask') from a microtask, before any timer.
  • Call log('macrotask') from a macrotask (timer).
JavaScript

⌘/Ctrl + Enter to run tests · Esc then Tab leaves the editor

Output
> Nothing has run yet.
Interview follow-up questions
  • Why does `await Promise.resolve()` in the test see the microtask result?
  • What's the difference between queueMicrotask and Promise.then?
  • What does the browser do between macrotasks that it doesn't do between microtasks?