In what order does JavaScript run synchronous and async code?
Synchronous code runs first on the call stack. Then the event loop drains the microtask queue (promises), then the macrotask queue (timers, events). This is why promise callbacks run before setTimeout callbacks.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in How JavaScript Executes Code Step by Step
Parsing (source to AST), compilation (AST to bytecode and machine code), execution context creation, synchronous code execution, async scheduling via Web APIs, and event loop draining queues.
It hands the timer to a Web API, continues running synchronous code, and when the timer fires the callback is pushed to the macrotask queue. The event loop runs it only after the stack is empty.
Because promise callbacks go to the microtask queue, which the event loop drains completely before touching the macrotask queue where setTimeout callbacks live.
Still have questions?
Browse all our FAQs or reach out to our support team
