Created
May 2, 2020 01:38
-
-
Save eMdOS/e49fd7eb4b57a95a81e9c12baf8fa953 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 extension URLSession { | |
typealias DataTaskResult = Result<(data: Data, response: URLResponse), Error> | |
typealias DataTaskResultCompletion = (DataTaskResult) -> Void | |
func dataTask( | |
with url: URL, | |
completion: @escaping DataTaskResultCompletion | |
) -> URLSessionDataTask { | |
dataTask(with: url) { (data, response, error) in | |
if let error = error { | |
completion(.failure(error)) | |
} else if let data = data, let response = response { | |
completion(.success((data: data, response: response))) | |
} | |
} | |
} | |
} | |
// then ... we can do something like: | |
URLSession.shared | |
.dataTask(with: url) { result in | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment