Created
May 31, 2021 07:47
-
-
Save alfianlosari/9972f99abd9f74683bfdeaf8262bc3e0 to your computer and use it in GitHub Desktop.
Fetch and decode using URLSession 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 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