Created
January 27, 2022 17:19
-
-
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
This file contains hidden or 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
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