Skip to content

Instantly share code, notes, and snippets.

@manas-sharma-1683
Last active March 3, 2021 08:58
Show Gist options
  • Save manas-sharma-1683/3bf5e02455c50a36c7d6f965e99a8da4 to your computer and use it in GitHub Desktop.
Save manas-sharma-1683/3bf5e02455c50a36c7d6f965e99a8da4 to your computer and use it in GitHub Desktop.
Combine + Result.
/*
*
* Use this when the publisher only publishes 1 value then stops.
*
*/
import Combine
import Foundation
extension Publisher {
func sink(result handler: @escaping (Result<Output, Failure>) -> Void) -> AnyCancellable {
return prefix(1).sink(receiveCompletion: { (completion) in
if case let .failure(error) = completion {
handler(.failure(error))
}
}, receiveValue: { (value) in
handler(.success(value))
})
}
}
var tokens = Set<AnyCancellable>()
somePublisher.sink { (result) in
switch result {
case .success(let payload):
print(payload)
case .failure(let error):
print(error)
}
}.store(in: &tokens)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment