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# 5.0, Python 3.5, Hack, Dart, Kotlin 1.1, Rust 1.39[1], Nim 0.9.4[2] and JavaScript ES2017, with some experimental work in extensions, beta versions, and particular implementations of Scala[3] and C++.
Example C#
The C# function below, which downloads a resource from a URI and returns the resource's length, uses this async/await pattern:
public async Task<int> FindPageSize(Uri uri)
{
byte[] data = await new WebClient().DownloadDataTaskAsync(uri);
return data.Length;
}
- First, the
async
keyword indicates to C# that the method is asynchronous, meaning that it may use an arbitrary number ofawait
expressions and will bind the result to a promise. - The return type,
Task<T>
, is C#'s analogue to the concept of a promise, and here is indicated to have a result value of typeint
. - The first expression to execute when this method is called will be
new WebClient().DownloadDataTaskAsync(uri)
, which is another asynchronous method returning aTask<byte[]>
. 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, unrejectedTask<byte[]>
to this function. - With the
await
keyword attached to theTask
, this function will immediately proceed to return aTask<int>
to its caller, who may then continue on with other processing as needed. - Once
DownloadDataTaskAsync()
finishes its download, it will resolve theTask
it returned with the downloaded data. This will trigger a callback and causeFindPageSize()
to continue execution by assigning that value todata
. - Finally, the method returns
data.Length
, a simple integer indicating the length of the array. The compiler re-interprets this as resolving theTask
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 await
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 you to await multiple promises at once or in some special pattern (such as C#'s Task.WhenAll()
, which returns a valueless Task
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:
public Task<int> FindPageSize(Uri uri)
{
Task<byte[]> data_task = new WebClient().DownloadDataTaskAsync(uri);
Task<int> after_data_task = data_task.ContinueWith((original_task) => {
return original_task.Result.Length;
});
return after_data_task;
}
Because of this, if an interface method needs to return a promise object, but itself does not require await
in the body to wait on any asynchronous tasks, it does not need the async
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 Task.FromResult()
), 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 await
to resolve. For instance, the following code, while always succeeding in a blocking model without await
, may experience intervening events during the await
and may thus find shared state changed out from under it:
var a = state.a;
var data = await new WebClient().DownloadDataTaskAsync(uri);
Debug.Assert(a == state.a); // Potential failure, as value of state.a may have been changed
// by the handler of potentially intervening event.
return data.Length;
In F#
An F# release of 2007 featured asynchronous workflows.[4]. In this initial version, await
was called let!
.
In C#
In C# versions before C# 7, async methods are required to return either void
, Task
, or Task<T>
. This has been expanded in C# 7 to include certain other types such as ValueTask<T>
. Async methods that return void
are intended for event handlers; in most cases where a synchronous method would return void
, returning Task
instead is recommended, as it allows for more intuitive exception handling.[5]
Methods that make use of await
must be declared with the async
keyword. In methods that have a return value of type Task<T>
, methods declared with async
must have a return statement of type assignable to T
instead of Task<T>
; the compiler wraps the value in the Task<T>
generic. It is also possible to await
methods that have a return type of Task
or Task<T>
that are declared without async
.
The following async method downloads data from a URL using await
.
public async Task<int> SumPageSizesAsync(IList<Uri> uris)
{
int total = 0;
foreach (var uri in uris) {
statusText.Text = string.Format("Found {0} bytes ...", total);
var data = await new WebClient().DownloadDataTaskAsync(uri);
total += data.Length;
}
statusText.Text = string.Format("Found {0} bytes total", total);
return total;
}
In Scala
In the experimental Scala-async extension to Scala, await
is a "method", although it does not operate like an ordinary method. Furthermore, unlike in C# 5.0 in which a method must be marked as async, in Scala-async, a block of code is surrounded by an async "call".
How it works
In Scala-async, async
is actually implemented using a Scala macro, which causes the compiler to emit different code, and produce a finite state machine implementation (which is considered to be more efficient than a monadic implementation, but less convenient to write by hand).
There are plans for Scala-async to support a variety of different implementations, including non-asynchronous ones.
In Python
Python 3.5 has added support for Async/Await as described in PEP0492 (https://www.python.org/dev/peps/pep-0492/).
In JavaScript
The await operator in JavaScript can only be used from inside an async function. 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.[6]
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.[7]
Here's an example (modified from this[8] article):
async function createNewDoc() {
let response = await db.post({}); // post a new doc
return await db.get(response.id); // find by id
}
async function main() {
try {
let doc = await createNewDoc();
console.log(doc);
} catch (err) {
console.log(err);
}
}
main();
Node.js version 8 includes a utility that enables using the standard library callback-based methods as promises.[9]
In C++
In C++, await (named co_await in C++) has been officially merged into C++20 draft, so it is on course to be formally accepted as a part of official C++20[10]; also MSVC and Clang compilers are already supporting at least some form of co_await (GCC still has no support for it).
#include <future>
#include <iostream>
using namespace std;
future<int> add(int a, int b)
{
int c = a + b;
co_return c;
}
future<void> test()
{
int ret = co_await add(1, 2);
cout << "return " << ret << endl;
}
int main()
{
auto fut = test();
fut.wait();
return 0;
}
In C
There's no officially supports for await/async in C language yet. Some coroutine libraries such as s_task simulates the keywords await/async with macros.
#include <stdio.h>
#include "s_task.h"
// define stack memory for tasks
int g_stack_main[64 * 1024 / sizeof(int)];
int g_stack0[64 * 1024 / sizeof(int)];
int g_stack1[64 * 1024 / sizeof(int)];
void sub_task(__async__, void* arg) {
int i;
int n = (int)(size_t)arg;
for (i = 0; i < 5; ++i) {
printf("task %d, delay seconds = %d, i = %d\n", n, n, i);
s_task_msleep(__await__, n * 1000);
//s_task_yield(__await__);
}
}
void main_task(__async__, void* arg) {
int i;
// create two sub-tasks
s_task_create(g_stack0, sizeof(g_stack0), sub_task, (void*)1);
s_task_create(g_stack1, sizeof(g_stack1), sub_task, (void*)2);
for (i = 0; i < 4; ++i) {
printf("task_main arg = %p, i = %d\n", arg, i);
s_task_yield(__await__);
}
// wait for the sub-tasks for exit
s_task_join(__await__, g_stack0);
s_task_join(__await__, g_stack1);
}
int main(int argc, char* argv) {
s_task_init_system();
//create the main task
s_task_create(g_stack_main, sizeof(g_stack_main), main_task, (void*)(size_t)argc);
s_task_join(__await__, g_stack_main);
printf("all task is over\n");
return 0;
}
In Perl5
The Future::AsyncAwait module was the subject of a Perl foundation grant in September 2018.[11]
In Rust
On November 7th 2019, async/await is now available on the stable version of Rust.[12]
Benefits and criticisms
A significant benefit of the async/await pattern in languages that support it is that asynchronous, non-blocking code can be written, with minimal overhead, and looking 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.[13] 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. Such applications range from programs presenting graphical user interfaces to massively scalable stateful server-side programs, such as games and financial applications.
When criticising await, it has been noted that await tends to cause surrounding code to be asynchronous too; on the other hand, it has been argued that this contagious nature of the code (sometimes being compared to a "zombie virus") is inherent to all kinds of asynchronous programming, so await as such is not unique in this regard.[5]
See also
- Coroutines
- Continuation-passing style
- Direct style
- Cooperative multitasking
References
- "Announcing Rust 1.39.0". Retrieved 2019-11-07.
- "Version 0.9.4 released - Nim blog". Retrieved 2020-01-19.
- "Scala Async". Retrieved 20 October 2013.
- "Introducing F# Asynchronous Workflows".
- Stephen Cleary, Async/Await - Best Practices in Asynchronous Programming
- "await - JavaScript (MDN)". Retrieved 2 May 2017.
- "jQuery Core 3.0 Upgrade Guide". Retrieved 2 May 2017.
- "Taming the asynchronous beast with ES7". Retrieved 12 November 2015.
- Foundation, Node.js. "Node v8.0.0 (Current) - Node.js". Node.js.
- "ISO C++ Committee announces that C++20 design is now feature complete".
- "September 2018 Grant Votes - The Perl Foundation". news.perlfoundation.org. Retrieved 2019-03-26.
- Matsakis, Niko. "Async-await on stable Rust!". Rust Blog. Retrieved 7 November 2019.
- 'No Bugs' Hare. Eight ways to handle non-blocking returns in message-passing programs CPPCON, 2018