Skip to content

Instantly share code, notes, and snippets.

@tomasharkema
Created June 10, 2021 13:20
Show Gist options
  • Save tomasharkema/f291dcb5b55bc7083c5eeb0049c4db75 to your computer and use it in GitHub Desktop.
Save tomasharkema/f291dcb5b55bc7083c5eeb0049c4db75 to your computer and use it in GitHub Desktop.
Await all in Swift 5.5
func whenAll<T>(tasks: [Task.Handle<T, Error>]) async throws -> [T] {
try await withThrowingTaskGroup(of: [T].self, body: { group in
for task in tasks {
group.async {
[try await task.get()]
}
}
return try await group.reduce([], +)
})
}
/*
whenAll(tasks: sections.map { s in
async {
(try await Api.shared.all(key: s.key)).MediaContainer.Metadata
}
})
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment