Skip to content

Instantly share code, notes, and snippets.

@HarshilShah
Created January 27, 2022 17:19
Show Gist options
  • Save HarshilShah/f5694fa340d3f440635f11d764155bc1 to your computer and use it in GitHub Desktop.
Save HarshilShah/f5694fa340d3f440635f11d764155bc1 to your computer and use it in GitHub Desktop.
A convenience initialiser for Effect that allows you to use Effect.task while still retaining strongly typed errors
extension Effect {
static func resultTask(
priority: TaskPriority? = nil,
operation: @escaping @Sendable () async -> Result<Output, Failure>
) -> Self {
Effect<Result<Output, Failure>, Never>
.task(priority: priority, operation: operation)
.flatMap { result -> Self in
switch result {
case .success(let value): return Effect(value: value)
case .failure(let error): return Effect(error: error)
}
}
.eraseToEffect()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment