Why do promise callbacks run before setTimeout callbacks in JavaScript?
Because promise callbacks go to the microtask queue, which the event loop drains completely before taking any macrotask from the macrotask queue where setTimeout callbacks live. Microtasks always run first.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in The Event Loop and Callbacks in JavaScript
When the call stack is empty, the event loop drains all microtasks (promise callbacks, queueMicrotask), then takes one macrotask (setTimeout, events). This repeats. Callbacks only run when the stack is empty.
Yes. If synchronous code is still running, the callbacks stay in the queue. The event loop cannot move them to the stack until it is empty. A 3-second blocking function delays all queued callbacks by 3 seconds.
Microtasks (promise callbacks, queueMicrotask) run after the current task and before the next macrotask. The event loop drains all microtasks before any macrotask. Macrotasks (setTimeout, events) run one at a time with microtasks drained after each.
Still have questions?
Browse all our FAQs or reach out to our support team
