Created
September 3, 2020 20:26
-
-
Save moreindirection/998e931e14176a04c80d64c6f1ae7f8a to your computer and use it in GitHub Desktop.
Branching with Combine
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
URLSession.shared.dataTaskPublisher(for: URL(string: BASE_URL + filename)!) | |
.flatMap { data, response in | |
let httpURLResponse = response as! HTTPURLResponse | |
switch httpURLResponse.statusCode { | |
case 404: | |
return Just("Unable to find file").eraseToAnyPublisher() | |
case (300..<399): | |
// this is a function that returns a publisher - it can make network requests, whatever | |
return processRedirect(data) | |
case (200..<299): | |
// now process data normally | |
default: | |
return Failure(errorType).eraseToAnyPublisher() | |
} | |
func processRedirect(redirectData: Data) -> AnyPublisher<String, Error> { | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment