tezvyn:

Node.js Callbacks: Functions That Run Later

AI-drafted, machine-checkedSource: nodejs.orgbeginner

A callback is a function you pass to run later when an event fires or work finishes, keeping Node.js free to handle other work. HTTP servers use them to respond to connections without blocking.

WHY IT EXISTS: Traditional servers used OS threads to handle many connections at once, but thread-based networking is inefficient and difficult to use correctly. Node.js was built to handle scalable network applications without threads, locks, or deadlocks by making I/O event-driven. Callbacks are the mechanism that lets the runtime say "tell me what to do when this finishes" instead of waiting around.

THE MENTAL MODEL: Think of a callback as a note you leave at a dry cleaner. You drop off your clothes, hand over a slip with your phone number, and leave to run other errands. When the cleaning is done, the shop calls you back. You do not stand in the shop blocking the counter while you wait. In Node.js, that slip is the callback function, and the runtime keeps serving other customers while work happens in the background.

HOW IT WORKS: You define behavior by passing a function as an argument to another function. Node.js holds that function in memory and fires it when the matching event occurs or the async operation completes. The runtime enters the event loop automatically after your script runs, and it exits only when there are no more callbacks left to perform. If there is no work to do, Node.js sleeps. This is different from systems like Ruby's Event Machine where you must make a blocking call such as EventMachine::run() to start the event loop.

WHEN TO USE IT: Use callbacks when you need to respond to I/O events such as HTTP requests, file system operations, or network sockets. They shine when you want many connections handled concurrently without spawning threads. Because almost no function in Node.js directly performs blocking I/O, callbacks let you build low-latency streaming servers that stay responsive under load.

WHEN NOT TO USE IT: Do not use callbacks when you actually need the result immediately before moving on, or when you are already inside a synchronous method from the Node.js standard library. Synchronous methods block the process, which freezes the event loop and stalls every pending callback. If your workload is CPU-bound rather than I/O-bound, callbacks alone will not help you utilize multiple cores.

ONE CANONICAL EXAMPLE: The Node.js hello world server demonstrates the pattern perfectly. You call createServer and pass a callback that runs every time an HTTP connection arrives. Inside that callback you set the status code to 200, write the plain text header, and end the response with Hello World. A second callback passed to server.listen logs that the server is running. The process never blocks waiting for connections; instead it sleeps when idle and fires the appropriate callback the moment a request comes in.

Read the original → nodejs.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.