Created
May 31, 2021 07:48
-
-
Save alfianlosari/9f85c0e73d8cacafb89e6761233d754b to your computer and use it in GitHub Desktop.
Update to fetch data with URL Session Async Function
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 fetchAPI<D: Decodable>(url: URL) async throws -> D { | |
let task = Task { () -> D in | |
try await fetchAndDecode(url: url) | |
} | |
//... | |
} | |
func fetchAPIGroup<D: Decodable>(urls: [URL]) async throws -> [D] { | |
try await withThrowingTaskGroup(of: D.self) { (group) in | |
for url in urls { | |
group.async { try await fetchAndDecode(url: url) } | |
} | |
//... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment