1, 4, 3, 2. Sync code runs first (1, 4). Then all microtasks (3). Then one macrotask (2). This is the fundamental event loop order.
Event Loop Interview Questions in JavaScript
What is the output of console.log('1'); setTimeout(() => console.log('2'), 0); Promise.resolve().then(() => console.log('3')); console.log('4')?
The answer
More FAQs in Event Loop Interview Questions in JavaScript
Code before await runs synchronously. await pauses the function and schedules the rest as a microtask. Code after the foo() call runs next (sync). Then the rest of foo runs (microtask). So: 1, 3, 2 for the example above.
Yes. The event loop drains all microtasks before rendering. If microtasks take too long, the paint is delayed. Keep microtasks short to maintain smooth rendering.
No. The event loop is part of the host environment (browser or Node.js). The JS engine (V8) has the call stack and heap. The event loop, Web APIs, and queues are provided by the host.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.