In what order do sync code, microtasks, and macrotasks run?
Sync code runs first on the call stack. When the stack is empty, the event loop drains all microtasks. Then it takes one macrotask. Then drains microtasks again. This repeats.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in setTimeout and the Event Loop Relationship Explained
setTimeout hands the timer to a Web API. When the timer fires, the callback is pushed to the macrotask queue. The event loop moves it to the call stack only when the stack is empty and all microtasks are done.
Because promise callbacks go to the microtask queue, which the event loop drains completely before taking one task from the macrotask queue where setTimeout callbacks live.
Yes. If synchronous code is still running, the callback stays in the macrotask queue. The event loop cannot run it until the stack is empty. A 3-second blocking function delays all queued timers by 3 seconds.
Still have questions?
Browse all our FAQs or reach out to our support team
