5 concepts · 10 practice questions
An async function can suspend at each await, giving its thread back to run other work, and resume later, possibly on a different thread. await marks every point where that can happen, so state read before an await may have changed after it. async let starts child tasks that run concurrently and are awaited together; a task group does the same for a number of children known only at runtime.
func loadDashboard() async throws -> Dashboard {
async let profile = api.profile() // starts now
async let feed = api.feed() // starts now, in parallel
return try await Dashboard(profile: profile, feed: feed)
}
func thumbnails(for urls: [URL]) async throws -> [UIImage] {
try await withThrowingTaskGroup(of: UIImage.self) { group in
for url in urls { group.addTask { try await loadImage(url) } }
return try await group.reduce(into: []) { $0.append($1) } // completion order
}
}
Sign in to read the rest and take the 10-question test.
Sign in to continue