async / await
Write asynchronous code that reads top-to-bottom, with errors handled by normal try/catch.
An async function can suspend while it waits (for the network, disk, or another task) without blocking a thread. You call it with await.
Use async let to run independent work in parallel and await all results together.
1struct Profile: Decodable { let name: String }
2struct Stats: Decodable { let followers: Int }
3
4func fetch<T: Decodable>(_ type: T.Type, from path: String) async throws -> T {
5 let url = URL(string: "https://api.example.com/\(path)")!
6 let (data, _) = try await URLSession.shared.data(from: url)
7 return try JSONDecoder().decode(T.self, from: data)
8}
9
10func loadDashboard() async throws -> (Profile, Stats) {
11 async let profile = fetch(Profile.self, from: "me")
12 async let stats = fetch(Stats.self, from: "me/stats")
13 return try await (profile, stats) // both requests run in parallel
14}Common pitfalls
- await does not mean "background thread" — isolation (actors, @MainActor) decides where code runs.
Still unclear? Ask BeeAI about async / await.
Get a tailored explanation, or paste your own code for a Swift 6 review. Free members get daily questions — Pro is unlimited.
Related topics
Start concurrent work, cancel it cooperatively, and fan out a dynamic number of child tasks.
Guarantee UI code runs on the main thread — and understand Swift 6.2 default MainActor isolation.
Start async work when a view appears — automatically cancelled when it disappears.
Become a Founding Pro member: unlimited BeeAI, upcoming Swift paths and every new reference entry first.
See Pro Plans