Skip to content
Learn Netverks

Lesson

Step 4/36 11% through track

event-loop-overview

The event loop (overview)

Last reviewed May 28, 2026 Content v20260528
Track mode
nodejs_server
Means
Node sandbox
Reading
~2 min
Level
beginner

This lesson

An orientation to the Node.js track—how the server playground works, core vocabulary, and what you will practice next.

The event loop explains why async I/O scales—blocking the loop freezes every concurrent request.

You will apply The event loop (overview) in contexts like: REST/GraphQL APIs, BFF layers, CLIs, webhooks, and real-time services (with WebSockets).

Run JavaScript on the Node runner when configured—never mix arbitrary shell commands in lessons.

After HTML fundamentals and basic programming concepts—before or alongside SQL.

Node's concurrency model is cooperative on a single main thread, backed by libuv thread pool and OS async I/O. Understanding the event loop explains why async code order surprises beginners and why blocking sync work hurts throughput.

Simplified phases

  1. Run synchronous code — top to bottom until the call stack is empty
  2. Microtasks — promise callbacks (.then, await continuations) run before the next macrotask
  3. MacrotaskssetTimeout, setImmediate, I/O callbacks
  4. Repeat — process pending work until the program exits

Classic ordering puzzle

console.log('A');
setTimeout(() => console.log('B'), 0);
Promise.resolve().then(() => console.log('C'));
console.log('D');
// A, D, C, B

Sync first, then microtasks (C), then timer (B).

What to avoid

Long synchronous loops, huge JSON parsing on hot paths, or sync fs.readFileSync under load block the loop—every other request waits. Prefer async APIs and offload CPU work to worker threads when needed.

Important interview questions and answers

  1. Q: Is Node multi-threaded?
    A: JavaScript runs on one main thread; libuv and worker threads handle some I/O and CPU work behind the scenes—the model feels single-threaded to your JS code.
  2. Q: Microtask vs macrotask?
    A: Promises run as microtasks before the next timer/I/O macrotask—explains why await resolves before setTimeout(fn, 0).
  3. Q: When use worker_threads?
    A: CPU-intensive tasks (image processing, heavy crypto) that would starve the event loop on the main thread.

Self-check

  1. In the puzzle above, why does C print before B?
  2. What happens if you run a 30-second sync loop on the main thread during a request?

Tip: Draw the event loop as a queue: synchronous code runs first, then microtasks (promises), then macrotasks (timers). Blocking the main thread blocks everything.

Interview prep

What blocks the Node event loop?

Synchronous CPU-heavy work, long loops, and blocking sync I/O on the main thread—use worker threads, queues, or async APIs instead.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Microtask vs macrotask?
  • Blocking the loop example?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump