Created
May 9, 2020 19:43
-
-
Save zarghol/4765b76dc39c7e972b23486aa6c74a86 to your computer and use it in GitHub Desktop.
custom publisher to avoid handling error in the sink
This file contains hidden or 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
extension Publisher { | |
func catchAndExit(_ completion: @escaping (Self.Failure) -> Void) -> AnyPublisher<Self.Output, Never> { | |
return self | |
.map { output -> Optional<Output> in output } | |
.catch { error -> Just<Optional<Output>> in | |
completion(error) | |
return Just(nil) | |
} | |
.filter { $0 != nil } | |
.map { $0! } | |
.eraseToAnyPublisher() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment