Skip to content

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

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