-
-
Save jasdev/7ae67582c06845e41d6007c721200971 to your computer and use it in GitHub Desktop.
Example `Result.Publisher` usage.
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
import Combine | |
import PlaygroundSupport | |
enum SomeError: Error { | |
case anError | |
} | |
func someThrowingFunction() throws { | |
guard Bool.random() else { throw SomeError.anError } /// For sake of example, `throw` on `false` values. | |
} | |
var subscriptions = Set<AnyCancellable>() | |
Result { try someThrowingFunction() } | |
.publisher | |
.sink( | |
receiveCompletion: { completion in | |
switch completion { | |
case .finished: | |
break | |
case .failure: | |
print("`someThrowingFunction` threw.") | |
} | |
}, | |
receiveValue: { print("`someThrowingFunction` didn’t error.") } | |
) | |
.store(in: &subscriptions) | |
PlaygroundPage.current.needsIndefiniteExecution = true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment