/replaceError- second variant Secret
Created
March 26, 2023 11:33
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
enum MyError: Error { | |
case errorOccurred | |
} | |
let firstPublisher = Fail<Int, MyError>(error: MyError.errorOccurred) | |
let secondPublisher = Just(10) | |
let publisher = firstPublisher | |
.replaceError(with: secondPublisher) | |
publisher.sink( | |
receiveCompletion: { print("Completion:", $0) }, | |
receiveValue: { print("Value:", $0) } | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In this example, we create two publishers, one that emits an error of type MyError and one that emits the value 10.
We then use the replaceError operator to replace any errors emitted by the first publisher with the values emitted by the second publisher. When we subscribe to the publisher, we'll receive the value 10 instead of an error.