Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jacobsapps/50312f8f684a67deca607c84a4a53c80 to your computer and use it in GitHub Desktop.
Save jacobsapps/50312f8f684a67deca607c84a4a53c80 to your computer and use it in GitHub Desktop.
// user IDs not known at compile-time
let userIds = getUserIds() // [1, 2, 3, 4, 5]
let users = await withTaskGroup(of: User.self) { group in
for id in userIds {
group.addTask {
await fetchUser(id: id)
}
}
var users: [User] = []
for await user in group {
users.append(user)
}
return users
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment