.agents/skills/swift-concurrency/references/async-await-basics.md
Use this when:
Skip this file if:
async let. Use tasks.md.async-sequences.md.Jump to:
Mark functions with async to indicate asynchronous work:
func fetchData() async -> Data {
// async work
}
func fetchData() async throws -> Data {
// async work that can fail
}
Key benefit over closures: The compiler enforces return values. No forgotten completion handlers.
Course Deep Dive: This topic is covered in detail in Lesson 2.1: Introduction to async/await syntax
Use Task to bridge from sync to async:
Task {
let data = try await fetchData()
}
Use await directly:
func processData() async throws {
let data = try await fetchData()
// process data
}
Structured concurrency executes top-to-bottom in the order you expect:
let first = try await fetchData(1) // Waits for completion
let second = try await fetchData(2) // Starts after first completes
let third = try await fetchData(3) // Starts after second completes
Code after await only executes once the awaited function returns.
Course Deep Dive: This topic is covered in detail in Lesson 2.2: Understanding the order of execution
Use async let to run multiple operations concurrently:
async let data1 = fetchData(1)
async let data2 = fetchData(2)
async let data3 = fetchData(3)
let results = try await [data1, data2, data3]
awaittry await in the async let line itself// Redundant - avoid this
async let data = try await fetchData()
// Correct - errors handled at await point
async let data = fetchData()
let result = try await data
Use when:
Avoid when:
TaskGroup)Course Deep Dive: This topic is covered in detail in Lesson 2.3: Calling async functions in parallel using async let
URLSession provides async alternatives to closure-based APIs:
// Closure-based (old)
URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { return }
// handle response
}.resume()
// Async/await (modern)
let (data, response) = try await URLSession.shared.data(for: request)
data or response to unwrapfunc fetchUser(id: Int) async throws -> User {
let url = URL(string: "https://api.example.com/users/\(id)")!
var request = URLRequest(url: url)
request.httpMethod = "GET"
let (data, response) = try await URLSession.shared.data(for: request)
guard let httpResponse = response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
throw NetworkError.invalidResponse
}
return try JSONDecoder().decode(User.self, from: data)
}
func createUser(_ user: User) async throws -> User {
let url = URL(string: "https://api.example.com/users")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try JSONEncoder().encode(user)
let (data, response) = try await URLSession.shared.data(for: request)
guard let httpResponse = response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
throw NetworkError.invalidResponse
}
return try JSONDecoder().decode(User.self, from: data)
}
Course Deep Dive: This topic is covered in detail in Lesson 2.4: Performing network requests using URLSession and async/await
Specify exact error types for better API contracts:
enum NetworkError: Error {
case invalidResponse
case decodingFailed(DecodingError)
case requestFailed(URLError)
}
func fetchData() async throws(NetworkError) -> Data {
do {
let (data, _) = try await URLSession.shared.data(from: url)
return data
} catch let error as URLError {
throw .requestFailed(error)
} catch {
throw .invalidResponse
}
}
Callers know exactly which errors to handle.
When converting closure-based code:
async, remove completion parameterlet user = try await fetchUser(id: 1)
let posts = try await fetchPosts(userId: user.id)
let comments = try await fetchComments(postIds: posts.map(\.id))
async let user = fetchUser(id: 1)
async let settings = fetchSettings()
async let notifications = fetchNotifications()
let (userData, settingsData, notificationsData) = try await (user, settings, notifications)
// Fetch user first (required for next step)
let user = try await fetchUser(id: 1)
// Then fetch related data in parallel
async let posts = fetchPosts(userId: user.id)
async let followers = fetchFollowers(userId: user.id)
async let following = fetchFollowing(userId: user.id)
let profile = Profile(
user: user,
posts: try await posts,
followers: try await followers,
following: try await following
)
For in-depth coverage of async/await patterns, error handling strategies, and real-world migration scenarios, see Swift Concurrency Course.