Created
June 7, 2022 17:59
-
-
Save markiv/1c06bb3be04c6283264c90a714b4c8a1 to your computer and use it in GitHub Desktop.
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
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