Skip to content

Instantly share code, notes, and snippets.

@hmlongco
Created August 24, 2025 22:43
Show Gist options
  • Save hmlongco/0cf68f3ec09a2a484aa2aaeba64fc299 to your computer and use it in GitHub Desktop.
Save hmlongco/0cf68f3ec09a2a484aa2aaeba64fc299 to your computer and use it in GitHub Desktop.
Modern FetchData
func fetchData(url: String) async throws -> Data {
guard let requestURL = URL(string: url) else {
throw NetworkError.invalidURL
}
let (data, _) = try await URLSession.shared.data(from: requestURL)
guard !data.isEmpty else {
throw NetworkError.noData
}
return data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment