Skip to content

Instantly share code, notes, and snippets.

@Ahmadalsofi
Created March 26, 2023 11:33
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) }
)
@Ahmadalsofi
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment