Async/await

In computer programming, the async/await pattern is a syntactic feature of many programming languages that allows an asynchronous, non-blocking function to be structured in a way similar to an ordinary synchronous function. It is semantically related to the concept of a coroutine and is often implemented using similar techniques, and is primarily intended to provide opportunities for the program to execute other code while waiting for a long-running, asynchronous task to complete, usually represented by promises or similar data structures. The feature is found in C#, C++, Python, F#, Hack, Julia, Dart, Kotlin, Rust, Nim, JavaScript, Swift and Zig.

History
F# added asynchronous workflows with await points in version 2.0 in 2007. This influenced the async/await mechanism added to C#.

Microsoft first released a version of C# with async/await in the Async CTP (2011). It was later officially released in C# 5 (2012).

Haskell lead developer Simon Marlow created the async package in 2012.

Python added support for async/await with version 3.5 in 2015 adding 2 new keywords,  and.

TypeScript added support for async/await with version 1.7 in 2015.

Javascript added support for async/await in 2017 as part of ECMAScript 2017 JavaScript edition.

Rust added support for async/await with version 1.39.0 in 2019 using the  keyword and the   postfix operator, both introduced in the 2018 edition of the language.

C++ added support for async/await with version 20 in 2020 with 3 new keywords,  ,.

Swift added support for async/await with version 5.5 in 2021, adding 2 new keywords  and. This was released alongside a concrete implementation of the Actor model with the  keyword which uses async/await to mediate access to each actor from outside.

Example C#
The C# function below, which downloads a resource from a URI and returns the resource's length, uses this async/await pattern:


 * First, the  keyword indicates to C# that the method is asynchronous, meaning that it may use an arbitrary number of   expressions and will bind the result to a promise.
 * The return type,, is C#'s analogue to the concept of a promise, and here is indicated to have a result value of type.
 * The first expression to execute when this method is called will be,   which is another asynchronous method returning a  . Because this method is asynchronous, it will not download the entire batch of data before returning. Instead, it will begin the download process using a non-blocking mechanism (such as a background thread), and immediately return an unresolved, unrejected   to this function.
 * With the  keyword attached to the , this function will immediately proceed to return a   to its caller, who may then continue on with other processing as needed.
 * Once  finishes its download, it will resolve the   it returned with the downloaded data. This will trigger a callback and cause   to continue execution by assigning that value to.
 * Finally, the method returns, a simple integer indicating the length of the array. The compiler re-interprets this as resolving the   it returned earlier, triggering a callback in the method's caller to do something with that length value.

A function using async/await can use as many  expressions as it wants, and each will be handled in the same way (though a promise will only be returned to the caller for the first await, while every other await will utilize internal callbacks). A function can also hold a promise object directly and do other processing first (including starting other asynchronous tasks), delaying awaiting the promise until its result is needed. Functions with promises also have promise aggregation methods that allow the program to await multiple promises at once or in some special pattern (such as C#'s,   which returns a valueless   that resolves when all of the tasks in the arguments have resolved). Many promise types also have additional features beyond what the async/await pattern normally uses, such as being able to set up more than one result callback or inspect the progress of an especially long-running task.

In the particular case of C#, and in many other languages with this language feature, the async/await pattern is not a core part of the language's runtime, but is instead implemented with lambdas or continuations at compile time. For instance, the C# compiler would likely translate the above code to something like the following before translating it to its IL bytecode format:

Because of this, if an interface method needs to return a promise object, but itself does not require  in the body to wait on any asynchronous tasks, it does not need the   modifier either and can instead return a promise object directly. For instance, a function might be able to provide a promise that immediately resolves to some result value (such as C#'s ), or it may simply return another method's promise that happens to be the exact promise needed (such as when deferring to an overload).

One important caveat of this functionality, however, is that while the code resembles traditional blocking code, the code is actually non-blocking and potentially multithreaded, meaning that many intervening events may occur while waiting for the promise targeted by an  to resolve. For instance, the following code, while always succeeding in a blocking model without, may experience intervening events during the   and may thus find shared state changed out from under it:

In F#
In 2007, F# added asynchronous workflows with version 2.0. The asynchronous workflows are implemented as CE (computation expressions). They can be defined without specifying any special context (like  in C#). F# asynchronous workflows append a bang (!) to keywords to start asynchronous tasks.

The following async function downloads data from an URL using an asynchronous workflow:

In C#
In 2012, C# added the async/await pattern in C# with version 5.0, which Microsoft refers to as the task-based asynchronous pattern (TAP). Async methods usually return either,  ,  ,      or. User code can define custom types that async methods can return through custom async method builders but this is an advanced and rare scenario. Async methods that return  are intended for event handlers; in most cases where a synchronous method would return , returning   instead is recommended, as it allows for more intuitive exception handling.

Methods that make use of  must be declared with the   keyword. In methods that have a return value of type, methods declared with   must have a return statement of type assignable to   instead of  ; the compiler wraps the value in the   generic. It is also possible to  methods that have a return type of   or   that are declared without.

The following async method downloads data from a URL using. Because this method issues a task for each uri before requiring completion with the  keyword, the resources can load at the same time instead of waiting for the last resource to finish before starting to load the next.

In Python
Python 3.5 (2015) has added support for async/await as described in PEP 492 (written and implemented by Yury Selivanov).

In JavaScript
The await operator in JavaScript can only be used from inside an async function or at the top level of a module. If the parameter is a promise, execution of the async function will resume when the promise is resolved (unless the promise is rejected, in which case an error will be thrown that can be handled with normal JavaScript exception handling). If the parameter is not a promise, the parameter itself will be returned immediately.

Many libraries provide promise objects that can also be used with await, as long as they match the specification for native JavaScript promises. However, promises from the jQuery library were not Promises/A+ compatible until jQuery 3.0.

Here's an example (modified from this article):

Node.js version 8 includes a utility that enables using the standard library callback-based methods as promises.

In C++
In C++, await (named co_await in C++) has been officially merged into version 20. Support for it, coroutines, and the keywords such as  are available in GCC and MSVC compilers while Clang has partial support.

It is worth noting that std::promise and std::future, although it would seem that they would be awaitable objects, implement none of the machinery required to be returned from coroutines and be awaited using co_await. Programmers must implement a number of public member functions, such as,  , and   on the return type in order for the type to be awaited on. Details can be found on cppreference.

In C
The C language does not support await/async. Some coroutine libraries such as s_task simulate the keywords await/async with macros.

In Perl 5
The Future::AsyncAwait module was the subject of a Perl Foundation grant in September 2018.

In Rust
On November 7, 2019, async/await was released on the stable version of Rust. Async functions in Rust desugar to plain functions that return values that implement the Future trait. Currently they are implemented with a finite state machine.

In Swift
Swift 5.5 (2021) added support for async/await as described in SE-0296.

Benefits and criticisms
The async/await pattern is especially attractive to language designers of languages that do not have or control their own runtime, as async/await can be implemented solely as a transformation to a state machine in the compiler.

Supporters claim that asynchronous, non-blocking code can be written with async/await that looks almost like traditional synchronous, blocking code. In particular, it has been argued that await is the best way of writing asynchronous code in message-passing programs; in particular, being close to blocking code, readability and the minimal amount of boilerplate code were cited as await benefits. As a result, async/await makes it easier for most programmers to reason about their programs, and await tends to promote better, more robust non-blocking code in applications that require it.

Critics of async/await note that the pattern tends to cause surrounding code to be asynchronous too; and that its contagious nature splits languages' library ecosystems between synchronous and asynchronous libraries and APIs, an issue often referred to as "function coloring". Alternatives to async/await that do not suffer from this issue are called "colorless". Examples of colorless designs include Go's goroutines and Java's virtual threads.