Await
In computer programming, await is a feature found in C# 5.0, Python 3.5, Hack, Dart, Kotlin 1.1, in an experimental extension for Scala,[1] and more recently JavaScript that allows an asynchronous, non-blocking method call to be performed in a similar way to an ordinary synchronous method call.
While a casual reading of the code would suggest that the method call blocks until the requested data is available, in fact it does not.
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.[2]
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.[3]
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.[4]
Here's an example (modified from this[5] 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);
}
}()
Node.js version 8 includes a utility that enables using the standard library callback-based methods as promises.[6]
See also
References
- ↑ "Scala Async". Retrieved 20 October 2013.
- ↑ "Async/Await - Best Practices in Asynchronous Programming". Retrieved 2 May 2017.
- ↑ "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.
- ↑ https://nodejs.org/en/blog/release/v8.0.0/#improved-support-for-promises