Introduction to Asynchronous Execution in Node.js

Node.js·2 min read·Jan 1, 2025

JavaScript is by design single-threaded, which means that each instruction is executed one after the other, in a sequence.

This synchronous behaviour, referred to as blocking, implies that JavaScript will wait for each task to complete before executing the next one.

While desirable in most cases, this sometimes leads to performance issues when it comes to handling time-consuming or concurrent tasks, like reading very large files or handling hundreds of incoming network requests.

Learning objectives

In this module, you'll learn how Node.js executes tasks asynchronously to avoid blocking the main thread, and how to write code that can handle operations that complete later (or fail).

You'll start by understanding the event loop, then you'll learn three essential ways of working with asynchronous code: timer functions, promises, and async / await. This will be a core skill for everything that comes next, because most real Node.js features are asynchronous by nature: file I/O, network requests, and databases.

After completing this module, you will be able to:

  • Explain what blocking means and why it becomes a problem in Node.js applications.
  • Describe what the event loop does at a high level and how it schedules background work.
  • Schedule delayed execution using timer functions such as setTimeout() and setInterval().
  • Handle asynchronous success and failure using promises.
  • Write asynchronous code using async and await without losing error handling.
  • Build scripts that periodically execute tasks on a schedule.