Skip to content

How Node.js Works: The Event Loop and Non-Blocking I/O

Beginning of the lesson

How Node.js Works: The Event Loop and Non-Blocking I/O

The single execution model that explains why async, timers, and streams behave the way they do

Big idea: Your JavaScript runs on one thread. Node stays fast not by running your code in parallel, but by never letting that one thread wait. When work needs the operating system (reading a file, querying a database, answering a socket), Node hands it off, keeps running other code, and comes back to your callback when the result is ready. The coordinator of that hand-off-and-return cycle is the event loop.
By the end you will be able to:
  • explain what runs on the single main thread and what does not;
  • trace how the call stack, the event loop, and its queues cooperate;
  • name the event loop's phases and what each one drains;
  • predict the order of setTimeout, setImmediate, Promise, and process.nextTick;
  • recognise blocking code and understand why it freezes an entire server.

One thread, one call stack

Node runs your JavaScript on a single thread with a single call stack. The call stack is a list of the functions that are currently running: when you call a function it is pushed on top, and when it returns it is popped off. Only the function on top is executing at any instant.

Because there is exactly one stack, two lines of your JavaScript can never truly run at the same moment. The engine (V8) takes one bite of work, runs it to completion, then takes the next. This is why you never see a variable half-updated by another thread: JavaScript code is run-to-completion. A function is not interrupted partway through by some other callback; that other callback has to wait until the stack is empty again.

This single-threaded model is the prerequisite for everything else in this lesson. If you remember one sentence, make it this: a callback can only start when the call stack is empty. The event loop's whole job is to decide which callback goes onto that empty stack next.

Analogy. Picture one chef in a kitchen (the thread). The chef can only do one action at a time. But a smart chef does not stand and stare at the oven while a cake bakes -- they set a timer, start chopping vegetables, and return to the cake when the timer rings. The oven, the timer, and the delivery driver are helpers outside the chef. The event loop is the chef's habit of always checking "what is ready for me now?" before starting the next task.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers