Effect.either
成功・失敗どちらも扱う
どちらのタスクが成功/失敗しても結果を処理したい場合に使う
code:ts
const program = Effect.race(
Effect.either(task1),
Effect.either(task2)
)
Effect.runPromise(program).then(console.log)
code:result
{
"_id": "Either",
"_tag": "Left",
"left": "task1"
}
https://effect.website/docs/error-management/expected-errors/#either
error型のあるやつをEffect.eitherで囲うとEither (effect)に変換してくれる
code:ts
const recovered = Effect.gen(function* () {
// program :: Effect<string, HttpError | ValidationError, never>
// failureOrSuccess :: Either<string, HttpError | ValidationError>
const failureOrSuccess = yield* Effect.either(program)
if (Either.isLeft(failureOrSuccess)) {
// Failure case: you can extract the error from the left property
const error = failureOrSuccess.left
return Recovering from ${error._tag}
} else {
// Success case: you can extract the value from the right property
return failureOrSuccess.right
}
})