Skip to main content

Menu

System onlineAI Tutorials / 2026

AI Tutorials
Knowledge search

Recent · Popular · Tutorials

  • dart3 tutorials
  • java1 tutorial
  • Javascript3 tutorials
  • Node.js Introduction2 tutorials
  • dartdart · General · In depth
  • JavascriptJavascript · Advanced · In depth
  • JavascriptJavascript · Advanced · Mid
  • JavascriptJavascript · Advanced · Short

Find a tutorial

Tell the assistant what you want to learn. It suggests published chapters.

For example: “I want to learn Rust ownership from the start.” The conversation is kept only in this tab.

0 / 2,000 characters

Enter to send, Shift+Enter for a new line. Do not include personal information.

Chapter 6

The Event-Driven, Non-Blocking Model

So far you have run small scripts that do one thing at a time: read a file, print a message, exit. Now we get to the idea that makes Node.js special — how it handles *many* things at once without slowing down. This chapter explains the model in plain words. You do not need to write code yet; the nex
Level: GeneralLength: Mid3 min read

Read at another depth

The Event-Driven, Non-Blocking Model

So far you have run small scripts that do one thing at a time: read a file, print a message, exit. Now we get to the idea that makes Node.js special — how it handles many things at once without slowing down. This chapter explains the model in plain words. You do not need to write code yet; the next chapters build on this picture.

The problem: waiting is expensive

Many tasks in a program take a long time not because the computer is busy, but because the program is waiting. Examples:

  • Reading a file from a slow disk
  • Asking a database a question
  • Fetching a page from the internet

While waiting, a traditional server often sets aside a whole worker — a chunk of memory and a separate thread of execution — just to sit idle. If one thousand people visit a website at once, that can mean one thousand idle workers. Memory fills up, and the server slows down or crashes.

The Node.js answer: one waiter, many orders

Node.js runs your JavaScript in a single thread — one line of instructions executed at a time. That sounds limiting, but imagine a restaurant with one extremely efficient waiter:

  1. Take an order from table 1.
  2. Hand it to the kitchen (the kitchen does the actual cooking).
  3. While the kitchen works, take orders from tables 2, 3, and 4.
  4. When food is ready, a bell rings, and the waiter delivers it.

The waiter never stands by the stove. The kitchen is Node.js's built-in machinery (things like file reading and network handling), which does the slow work in the background.

The two key words

  • Event-driven: your program reacts to things that happen — a file finished loading, a request arrived, a timer ran out. Each of those happenings is an event. You register a function (a callback) to run when an event occurs, like telling the waiter "when the food is ready, bring it to my table."
  • Non-blocking: while one task waits, your program does not stop and hold everyone up. It moves on. "Blocking" is when code makes everything else wait; Node.js APIs are designed to avoid that.

Why this matters to you

Because of this model, a small Node.js program can serve thousands of simultaneous users on modest hardware. It is a big reason Node.js is popular for web servers, chat apps, and tools that talk to many services at once.

It also shapes how you write code: instead of "do step 1, then step 2, then step 3," you often say "start step 1, and here is a function to run when it finishes." The next chapters show friendly modern ways to do this, so the callback style never feels tangled.

Quick check

  • Single thread = one waiter. Slow work (I/O) is handed to the background "kitchen."
  • Events are things that happen; callbacks are the functions you attach to them.
  • Non-blocking means waiting never stops other work from proceeding.

Exercise

A user opens your app at the same moment as 500 other users. Why does a Node.js server not need 500 threads?

Show answer

Node.js uses one thread to accept all the requests and hands slow work (reading files, network calls) to background machinery. When each piece of work finishes, an event triggers a callback, and the single thread delivers each result. No thread sits idle waiting.

Exercise

Name one kind of task that is a poor fit for Node.js's single thread.

Show answer

Long, pure computations — for example, crunching a huge dataset in a loop. That blocks the single thread, so all other events (like incoming requests) queue up and wait.

Spotted a mistake, or code that does not work?

Report an issue

Chapter: The Event-Driven, Non-Blocking Model

0 / 2,000 characters

Plain text, for example: the second code sample does not compile. Do not include personal information.

You can't do that