Asynchronous Programming
Also known as: Async programming, Non-blocking programming
Programming approach that lets a program start long-running operations, such as network requests, and continue other work until the results arrive.
Asynchronous programming is a way of structuring code so that slow operations do not force the whole program to wait. In a synchronous program, a call to read a file or fetch data over the network blocks: nothing else happens until the result comes back. In an asynchronous program, the operation is started, the program moves on and the result is handled later, when it becomes available. A web page can thus keep responding to clicks while data loads, and a server can handle thousands of open connections without dedicating a thread to each one.
Several mechanisms express this. Callbacks pass a function to be called when an operation finishes, but deeply nested callbacks become hard to follow. Promises, also called futures, represent a value that will exist later and can be chained. The async and await keywords, popularized by C# in 2012 and later adopted by JavaScript, Python, Rust and others, let asynchronous code be written in a style that reads almost like ordinary sequential code. Underneath, many systems run an event loop that hands each completed operation to the code waiting for it.
Asynchronous programming is a form of concurrency but not necessarily of parallelism: a single thread can juggle many waiting tasks without running any two at the same instant. It suits work dominated by input and output, such as web servers, user interfaces and API clients, and is central to Node.js and browser JavaScript. It adds complexity: errors can surface far from their cause, the order of events becomes harder to predict and CPU-heavy work can still block an event loop. Mixing synchronous and asynchronous code carelessly is a common source of bugs.
Key points
- Asynchronous code starts slow operations and continues with other work instead of blocking until they finish.
- Callbacks, promises and async/await are successive ways of expressing asynchronous logic more readably.
- Asynchrony enables concurrency on a single thread but does not by itself make code run in parallel.
- It is best suited to work dominated by waiting on networks, disks or users rather than heavy computation.
Related entries
Event Loop
Programming construct that repeatedly waits for events, such as user input or completed network requests, and dispatches each to its handler.
Concurrency
Ability of a program or system to make progress on several tasks during overlapping time periods, whether or not they literally run simultaneously.
Parallel Computing
Form of computation in which many calculations run simultaneously on multiple processors or cores to solve problems faster or at larger scale.
JavaScript
High-level programming language of the web, created at Netscape in 1995, that runs in every major browser and on servers through runtimes like Node.js.
Node.js
Open-source JavaScript runtime built on Google's V8 engine that runs JavaScript outside the browser using an event-driven, non-blocking model.
Closure
Function bundled with references to variables from the scope where it was created, letting it keep using them after that scope has finished.
Race Condition
Flaw in which a system's behavior depends on the uncontrolled timing or ordering of events, such as two threads updating the same data at once.
WebSocket
Communication protocol that provides a persistent, two-way channel between a web browser and a server over a single TCP connection.
Referenced by
Other entries that discuss or point to Asynchronous Programming.