Skip to content

Instantly share code, notes, and snippets.

@jasdev
Created April 7, 2020 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdev/7ae67582c06845e41d6007c721200971 to your computer and use it in GitHub Desktop.
Save jasdev/7ae67582c06845e41d6007c721200971 to your computer and use it in GitHub Desktop.
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