Structured concurrency, async functions
Define async functions, call them with await , and understand structured concurrency : child tasks finish before the parent scope exits.
Why async/await?
async/await lets you write asynchronous code that reads like sync code. With structured concurrency, child tasks are tied to scope lifetimes—no leaks.
Your first async function
An async function can await other async work. Create a Task in scripts/playgrounds to call it.
// Simulate a short async delay, then return a value
func fetchNumber() async -> Int {
try? await Task.sleep(nanoseconds: 200_000_000) // ~200ms
return 42
}
// Call sites must use "await" from an async context or a Task
Task {
let n = await fetchNumber()
print("Fetched:", n) // 42
}All lessons in this course
- Structured concurrency, async functions
- async let, Task, cancellation
- try await and error propagation