Last active
March 3, 2021 08:58
-
-
Save manas-sharma-1683/3bf5e02455c50a36c7d6f965e99a8da4 to your computer and use it in GitHub Desktop.
Combine + Result.
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
/* | |
* | |
* 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