Async and await¶
Category: Async · Status: stub · Lessons: chapter 06, Async (planned)
One line: Syntax that lets asynchronous code read like sequential code: an async function returns a future, and await pauses the caller until that future resolves, without blocking the thread.
Also called: async/await, async function, coroutines (C++20).
How it connects¶
flowchart LR
n_async_await["Async and await"]
n_future_and_promise["Future and promise"]
n_suspension_point["Suspension point"]
n_async_await -->|uses| n_future_and_promise
n_async_await -->|uses| n_suspension_point
classDef center stroke-width:3px
class n_async_await center
classDef outside stroke-dasharray: 4 3
class n_future_and_promise,n_suspension_point outside
- Is built on: Future and promise, Suspension point
- See also: Async functions as state machines, Asynchrony, Coroutine, Durable execution, Function coloring, Future and promise, Suspension point
In each language¶
| Rust | async fn and .await; an async function does no work until its future is polled (reference ↗), and while most languages with async bundle a runtime, Rust does not (Book ↗) |
| Go | neither: code blocks in ordinary calls, and when a goroutine blocks the runtime moves the others to a runnable thread (FAQ ↗) |
| C++ | C++20 coroutines ↗ (co_await, co_yield, co_return) are stackless; C++20 ships handles and traits but no task type, which the support library ↗ adds as std::execution::task in C++26 |
| Java | no async/await: JEP 444 ↗ delivered virtual threads in JDK 21 and rejected async/await because it would split the world between APIs for threads and APIs for coroutines; CompletableFuture ↗ chains actions on completion |
| Python | async def and await ↗ on asyncio's event loop ↗; simply calling a coroutine does not schedule it (docs ↗) |
| C# | an async method returns a Task ↗ that is already running: only tasks made with a Task constructor start cold (TAP ↗); the compiler turns the method into a state machine |
| JavaScript | an async function ↗ runs synchronously up to its first await, so its work starts on the call; await ↗ is allowed only in async functions and at the top level of a module |
| Kotlin | suspend functions, called with no await keyword and only from other suspending functions (basics ↗); async ↗ returns a Deferred to await, and starts lazily only if asked to |
| Swift | async functions with await marking each possible suspension point, plus async let and task groups (Swift book ↗) |
Where to read more¶
- In this library: Is total += n safe on two threads?
- In the books: Asynchronous Programming in Rust, Carl Fredrik Samson — ch. 7, 'Coroutines and async/await'
- In the books: Pro Asynchronous Programming with .NET, Richard Blewett, Andrew Clymer — ch. 7, 'async and await'
- In the books: asyncio Recipes, Mohamed Mustapha Tahrioui — ch. 3, 'Working with Coroutines and Async/Await'
- In the books: Kotlin Coroutines by Tutorials, Filip Babić, Nishant Srivastava — ch. 5, 'Async/Await'
- In the books: Async JavaScript, Trevor Burnham — ch. 1, 'Understanding JavaScript Events' → 'Types of Async Functions'
- In the books: Combine: Asynchronous Programming with Swift, Shai Mishali, Florent Pillet, Marin Todorov, Scott Gardner — ch. 2, 'Publishers & Subscribers' → 'Bridging Combine publishers to async/await'
- Notes: Topics - async - how to teach async rust ↗
- Notes: async - rust - concurrency - main - idx ↗
- Notes: Asynchronous Programming in Rust (notes) ↗
- Notes: book rust asynchronous programming ↗
- Notes: Error handling in async functions ↗
- Notes: asynchronous python ↗
- Notes: async kata - python ↗
- Notes: async - concurrent - C# - main ↗
- Notes: async method - c# ↗
- Notes: async - dart programming - main ↗
- Reference: Wikipedia: Async/await ↗