Skip to content

Instantly share code, notes, and snippets.

@alfianlosari
Created May 31, 2021 07:47
Show Gist options
  • Save alfianlosari/9972f99abd9f74683bfdeaf8262bc3e0 to your computer and use it in GitHub Desktop.
Save alfianlosari/9972f99abd9f74683bfdeaf8262bc3e0 to your computer and use it in GitHub Desktop.
Fetch and decode using URLSession async function
func fetchAndDecode<D: Decodable>(url: URL) async throws -> D {
let data = try await URLSession.shared.data(with: url)
let decodedData = try JSONDecoder().decode(D.self, from: data)
return decodedData
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment