Skip to content

Instantly share code, notes, and snippets.

@markiv
Created June 7, 2022 17:59
Show Gist options
  • Save markiv/1c06bb3be04c6283264c90a714b4c8a1 to your computer and use it in GitHub Desktop.
Save markiv/1c06bb3be04c6283264c90a714b4c8a1 to your computer and use it in GitHub Desktop.
public struct Backport<Portee> {
let portee: Portee
}
public extension URLSession {
var backport: Backport<URLSession> {
Backport(portee: self)
}
}
public extension Backport where Portee == URLSession {
@available(iOS, deprecated: 15.0, message: "This backport is no longer needed since iOS 15. Use data(from:) directly.")
func data(from url: URL) async throws -> (Data, URLResponse) {
try await withCheckedThrowingContinuation { continuation in
portee.dataTask(with: url) { data, response, error in
if let data = data, let response = response {
continuation.resume(returning: (data, response))
} else {
continuation.resume(throwing: error ?? URLError(.badServerResponse))
}
}
.resume()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment