Skip to content

Instantly share code, notes, and snippets.

@alfianlosari
Created May 31, 2021 07:48
Show Gist options
  • Save alfianlosari/9f85c0e73d8cacafb89e6761233d754b to your computer and use it in GitHub Desktop.
Save alfianlosari/9f85c0e73d8cacafb89e6761233d754b to your computer and use it in GitHub Desktop.
Update to fetch data with URL Session Async Function
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