The JavaScript Event Loop: A Core Interview Topic for React, Angular, Vue, and Node.js

  • Home
  • The JavaScript Event Loop: A Core Interview Topic for React, Angular, Vue, and Node.js
image

The JavaScript Event Loop: A Core Interview Topic for React, Angular, Vue, and Node.js

  • CategoryIT & Software
  • Views0
  • PublishedNovember 22, 2026
5 min read

1. The call stack: what is executing now

The call stack tracks active execution contexts. Calling a function pushes a context onto the stack; returning from it removes that context. Nested calls therefore follow a last-in, first-out order.

On a given JavaScript thread, queued callbacks cannot interrupt the current synchronous execution. A long-running function delays the callbacks waiting to execute on that thread.

Browsers and Node.js provide timers, networking, and other asynchronous facilities. Those operations can progress outside the JavaScript call stack, but their JavaScript callbacks still need an opportunity to run.

2. Tasks and microtasks: two scheduling mechanisms

In browsers, timers such as setTimeout() schedule callbacks as tasks once the timer's delay requirements are satisfied. User interactions and other browser activity also produce tasks.

Promise reaction handlers, including applicable .then(), .catch(), and .finally() callbacks, run as microtasks. queueMicrotask() explicitly adds a callback to the same microtask queue.

For a typical browser task, the useful model is:

Run the task's synchronous JavaScript.

At the microtask checkpoint, drain the microtask queue.

Allow rendering when a rendering opportunity occurs.

Continue with another runnable task.

The browser is not required to paint after every task. It can also maintain multiple task queues, so a single globally ordered callback queue is an oversimplification.

Microtasks execute on the call stack, just like other JavaScript callbacks. They do not run on a separate background JavaScript thread.

3. Promises and queueMicrotask(): order follows enqueue time

Promise handlers and queueMicrotask() callbacks share the microtask queue. Their order depends on when they enter that queue.

There are two important distinctions:

The executor passed to new Promise() runs synchronously.

The applicable Promise handlers execute asynchronously, even when the Promise is already settled.

A pending Promise does not yet have a runnable fulfillment handler. If it settles inside a later timer callback, its fulfillment handlers cannot run before that timer simply because they are microtasks.

4. A practical execution-order example

Consider this code, executed together as a script:

The output is:

1, 2, 3, 4, 5, 6, 7, 8

The sequence follows directly from the scheduling rules:

1, 2, 3: synchronous code runs first. The Promise executor prints 2 immediately, before the final synchronous statement prints 3.

4: the first .then() handler was queued before the microtask that prints 5.

5: this microtask was already waiting when the first .then() handler scheduled the callback that prints 6.

6: the microtask created inside the first .then() handler now executes.

7: when the first .then() handler returns, the Promise created by that .then() fulfills. This queues the second handler after the microtask that prints 6.

8: the timer callback runs after this microtask checkpoint has finished.

The important interview skill is tracking when each callback becomes eligible and enters its queue. An entire Promise chain is not queued upfront.

5. Why setTimeout(fn, 0) is not immediate

A zero-delay timeout schedules a timer; it does not interrupt the current execution or bypass pending microtasks.

The requested delay is a scheduling threshold, not a guaranteed execution time. Busy JavaScript, timer clamping, and browser throttling can all postpone the callback.

6. Microtasks and application responsiveness

At a microtask checkpoint, the runtime continues draining the queue until it is empty. Microtasks scheduled by other microtasks are processed during that same checkpoint.

This can become a performance problem. A callback that continually schedules another microtask can prevent the checkpoint from finishing, delaying timers and browser rendering.

Similarly, moving expensive computation into a Promise handler does not move it off the JavaScript thread. The computation still blocks other work while it executes.

The code after await resumes through Promise scheduling. Even await Promise.resolve() suspends the async function, but repeatedly awaiting fulfilled Promises does not reliably give the browser an opportunity to paint.

For substantial CPU work, consider splitting work across tasks or using Web Workers in browsers and worker threads in Node.js. Choose the approach based on the workload and the runtime.

7. Node.js: the execution context matters

Node.js organizes timers and I/O callbacks into event-loop phases. It also provides process.nextTick() and setImmediate(), which require additional reasoning.

At CommonJS top level, process.nextTick() callbacks run before Promise and queueMicrotask() callbacks. At ES module top level, module evaluation is already occurring through microtask processing, which changes that ordering.

The order between setTimeout(fn, 0) and setImmediate(fn) is also context-dependent. When both are scheduled from an I/O callback, setImmediate() runs first; scheduling them at top level does not provide the same universal ordering guarantee.

Before predicting Node.js output, identify the module type and where the callbacks are scheduled.

8. Why this matters for framework interviews

In React, Angular, and Vue applications, distinguish callback execution, the framework applying an update, and the browser painting the result. These are separate milestones. The event loop provides the foundation for understanding each framework's scheduling and update behavior.

In Node.js applications, the same principles explain why CPU-heavy callbacks can delay unrelated requests, even when the surrounding application uses asynchronous I/O.

A strong interview answer traces synchronous execution, records when callbacks are queued, drains microtasks at the appropriate checkpoints, and accounts for runtime-specific rules. That reasoning is equally valuable when diagnosing real application behavior.

References

HTML Standard: Event loops

https://html.spec.whatwg.org/multipage/webappapis.html#event-loops

MDN: Using microtasks in JavaScript with queueMicrotask()

https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide

MDN: Promise() constructor

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise

MDN: setTimeout()

https://developer.mozilla.org/en-US/docs/Web/API/Window/setTimeout

MDN: await

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

Node.js: Event loop, timers, and process.nextTick()

https://nodejs.org/learn/asynchronous-work/event-loop-timers-and-nexttick

Node.js: queueMicrotask() versus process.nextTick()

https://nodejs.org/api/process.html#when-to-use-queuemicrotask-vs-processnexttick

Armen Melkumyan

Technical / Solutions Architect