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 2

The Event-Driven, Non-Blocking Model

Node.js works differently from many programs you may know. Instead of doing one thing at a time and waiting for each step to finish, it starts a task, moves on, and comes back when the task is done. This is called **non-blocking I/O**. "I/O" means input and output — reading files, talking to the net
Level: GeneralLength: Short2 min read

Read at another depth

The Event-Driven, Non-Blocking Model

Node.js works differently from many programs you may know. Instead of doing one thing at a time and waiting for each step to finish, it starts a task, moves on, and comes back when the task is done. This is called non-blocking I/O. "I/O" means input and output — reading files, talking to the network, and so on. "Non-blocking" means Node does not sit still while waiting.

Think of a restaurant cook. A blocking cook takes one order, cooks it, serves it, then takes the next order. A non-blocking cook takes all the orders at once, puts food on the stove, and plates each dish as it becomes ready.

How the Event Loop Works

The event loop is the part of Node.js that keeps this moving. It is a simple repeating cycle:

  1. Node starts a task, like reading a file.
  2. While the task runs, Node handles other work.
  3. When the task finishes, it joins a queue — a waiting line.
  4. The event loop picks finished tasks from the queue and runs their follow-up code.

You do not write the event loop. You just hand Node functions to run when things finish. Those functions are called callbacks — functions that "call back" when the work is done.

Code example in javascript
const { readFile } = require('node:fs/promises');

console.log('Start');

async function main() {
  const text = await readFile('notes.txt', 'utf8');
  console.log('File read!');
}

main();
console.log('End');

"End" prints before "File read!" because reading the file happens in the background while the rest of the code runs.

Organizing Code with Modules

A module is a file that shares its code with other files. Node programs are split into modules to stay tidy.

Code example in javascript
// greet.js
function greet(name) {
  return `Hello, ${name}!`;
}
module.exports = greet;
Code example in javascript
// app.js
const greet = require('./greet');
console.log(greet('Node'));

module.exports says what the file gives out. require pulls that in.

Managing Dependencies with npm

You do not have to build everything yourself. npm is Node's package manager — a tool that downloads ready-made code, called packages, that others have shared.

Run npm init to create a package.json file. This file lists your project's packages, like a shopping list. Then npm install <name> adds a package and records it in the list. Anyone with your package.json can reinstall everything with one command: npm install.

Exercise

Why does "End" print before "File read!" in the example above?

Show answer

Reading the file is non-blocking. Node starts the read, continues running the rest of the script, and the event loop runs the follow-up code only when the file is ready.

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