Skip to content

Instantly share code, notes, and snippets.

@zarghol
Created May 9, 2020 19:43
Show Gist options
  • Save zarghol/4765b76dc39c7e972b23486aa6c74a86 to your computer and use it in GitHub Desktop.
Save zarghol/4765b76dc39c7e972b23486aa6c74a86 to your computer and use it in GitHub Desktop.
custom publisher to avoid handling error in the sink
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