Facebook Pixel

In what order do sync code, microtasks, and macrotasks run in JavaScript?

Sync code runs first on the call stack. When the stack is empty, the event loop drains all microtasks (promise callbacks, queueMicrotask). Then it takes one macrotask (setTimeout, setInterval, I/O). 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: Their Relationship in JavaScript

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. Microtasks always run before macrotasks.

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

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.