Asynchronous programming in Node.js means starting an operation, such as reading a file or calling an API, and continuing with other work while it runs, instead of waiting for it to finish. When the result is ready, Node.js runs your callback, resolves your promise or resumes your async function. This is what lets one thread serve thousands of connections, and it is the core idea behind asynchronous programming in Node.js.
Your JavaScript code runs on a single main thread. The slow work, such as disk and network input and output, is handed to the operating system or to a pool of background threads, so the main thread stays free.
A web server spends most of its time waiting: for a database to answer, for a file to load, for a client to send the next bytes. If each request blocked the thread while waiting, a second visitor would have to wait for the first one to finish.
Node.js avoids this with non-blocking input and output. When you ask for a file, Node.js starts the read, registers what should happen next, and goes on to the next task. The event loop, which a related article explains in detail, picks up the finished result and runs your code.
Compare the order of the output with the order of the lines.
The call to readFile() returns immediately. The line 2. end of script runs before the file is read, and the callback runs later. Code that depends on the file must therefore live inside the callback, or in whatever replaces it.
Node.js code uses three styles to handle a result that arrives later. They do the same job.
A callback is a function passed to another function. Node.js uses the error-first convention: the first argument is an error or null, and the following arguments hold the result. The example above shows it. Callbacks are simple, but they nest deeply when steps depend on each other, a problem known as callback hell.
A promise is an object that represents a value that will exist later. It is pending, then either fulfilled with a value or rejected with an error. You chain steps with then() and handle failures with catch().
The node:fs/promises module offers promise versions of the file system functions. Many other core modules have promise versions too.
An async function always returns a promise, and await pauses that function (not the whole program) until a promise settles. The code reads from top to bottom like synchronous code.
Use try and catch for errors, as you would in synchronous code. Top-level await works in ES modules.
Awaiting one operation after another runs them in sequence. If they do not depend on each other, start them together and wait for all results with Promise.all().
If any promise rejects, Promise.all() rejects immediately. Use Promise.allSettled() when you want every result, whether it succeeded or failed.
| Mistake | What happens | Fix |
|---|---|---|
Forgetting await | You get a pending promise, not the value | Add await or use then() |
Using await in a loop for independent tasks | Slow, sequential work | Use Promise.all() |
| Throwing inside a callback | The error cannot be caught by an outer try | Pass it to the callback or use promises |
No catch on a promise | An unhandled rejection can stop the process | Always handle rejections |
| Mixing styles in one function | Confusing control flow | Pick promises with async and await |
Since Node.js 15, an unhandled promise rejection ends the process with an error by default, so missing error handling is not just a bad habit.
Asynchronous does not mean multi-threaded JavaScript. Your own code still runs one piece at a time, so a long loop of calculations blocks everything else, even inside an async function. For heavy computation, use worker threads or a separate process. Asynchronous programming helps with waiting, not with computing.
Your JavaScript runs on one main thread, but Node.js uses other threads internally for some work, such as file system operations and DNS lookups, through libuv.
Use promises with async and await for new code. Callbacks remain in older APIs, and util.promisify() can convert them.
It makes your program handle more work at the same time while waiting. A single operation takes just as long, but the thread is free to do other work in the meantime.
In current Node.js versions the process prints the error and exits with a non-zero code, unless you register an unhandledRejection handler.
Asynchronous programming in Node.js starts slow operations without stopping the main thread and handles the results later. Callbacks, promises and async/await are three ways to express that. Prefer async and await, run independent work with Promise.all(), handle every error, and remember that long calculations still block the thread.