Skip to content

asynchronous programming

Asynchronous programming is a way of writing programs that start a slow operation, such as a network request or a file read, and keep running other code while it completes, then handle its result later.

JavaScript runs a program’s code on a single thread, so code that sat waiting for the network would freeze everything else. Instead, the host environment, such as a browser or Node.js, does the waiting. When the operation completes, the host queues a job, and the event loop runs that job once the call stack is empty.

ECMA-262 defines promises, async functions, and jobs, but the operations that wait, such as timers, file reads, and network requests, come from the host.

Code receives the later result in one of three common ways. Older APIs take a callback that the host calls when the work is done. Most current APIs, such as fetch(), return a promise. An async function can pause at await, so code that depends on promises reads from top to bottom.

Asynchronous code still shares the single JavaScript thread, and it saves time only while the host waits on something outside the program. A long calculation blocks the thread even inside a promise, unless it moves to a separate thread such as a web worker.

Example

Say a command-line task tracker loads its task list from a file and prints a banner at startup. The program starts the file read, keeps going, and uses the text only once it arrives:

Language: Text Filename: tasks.txt
Write tests
Fix login bug
Update docs
Language: JavaScript Filename: list-tasks.mjs
import { readFile } from "node:fs/promises";

const tasksFile = readFile("tasks.txt", "utf8");
console.log("File read:", tasksFile);
console.log("Task Tracker 1.0");

const tasks = (await tasksFile).trim().split("\n");
console.log(`Tasks loaded: ${tasks.length}`);

Running the module shows the banner printing while the read is still pending:

Language: Shell
$ node list-tasks.mjs
File read: Promise { <pending> }
Task Tracker 1.0
Tasks loaded: 3

The readFile() call hands the read to Node.js and returns a pending promise at once, so the banner prints while the disk works. The top-level await pauses the module until the promise fulfills, and the event loop resumes it with the file’s text.

Step through the module to watch Node.js read the file while the program keeps running:

Interactive diagram — enable JavaScript to view.
A developer feeds task cards into a machine with a NOW chute, a LOOP dial, and a WORKER booth where a small robot processes cards, under a large timer clock.

Course

The JavaScript Event Loop, Concurrency, and Workers

Build a weather-station dashboard in JavaScript, and learn how the event loop orders timers and promises, how to cancel work with AbortController, and when to move CPU work to a worker.

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 22, 2026