mediumAmazonDSA2024 · 2022

How does the Node.js event loop work?

2.9k views
01

Understand the problem

Walk through the phases of the Node.js event loop and where microtasks fit.

nodeevent-loopasync
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Stuck? AI Nudge Available

Get a conceptual hint to guide your logic without spoiling the final implementation.

03

Study the solution

The solution is waiting

Give it an honest attempt first — then compare your thinking with the full walkthrough.

04

Read the code

Microtasks always finish before the next macrotask
setTimeout(() => console.log('macro: timeout'), 0);

Promise.resolve()
  .then(() => console.log('micro 1'))
  .then(() => console.log('micro 2'));   // chained microtasks

// Output: micro 1, micro 2, then "macro: timeout"
// Both microtasks drain before the loop advances to the timer phase.
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.