Skip to content

Instantly share code, notes, and snippets.

@jasdev jasdev/result_publisher.swift Secret
Created Apr 7, 2020

Embed
What would you like to do?
Example `Result.Publisher` usage.
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
You can’t perform that action at this time.