Created
June 10, 2021 13:20
-
-
Save tomasharkema/f291dcb5b55bc7083c5eeb0049c4db75 to your computer and use it in GitHub Desktop.
Await all in Swift 5.5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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