Hyperlinkv0.8.0-beta.28

Result

Result.orElseconsteffect/Result.ts:1292
<E, A2, E2>(that: (err: E) => Result<A2, E2>): <A>(
  self: Result<A, E>
) => Result<A | A2, E2>
<A, E, A2, E2>(
  self: Result<A, E>,
  that: (err: E) => Result<A2, E2>
): Result<A | A2, E2>

Returns the original Result if it is a Success, otherwise applies that to the error and returns the resulting Result.

When to use

Use when a failure should recover into another Result while keeping successes unchanged.

Details

  • Success<A> is returned unchanged
  • Failure<E> calls that(e) to produce a new Result

Example (Recovering from a failure)

import { pipe, Result } from "effect"

const result = pipe(
  Result.fail("primary failed"),
  Result.orElse(() => Result.succeed(99))
)
console.log(result)
// Output: { _tag: "Success", success: 99, ... }
error handlinggetOrElsemapError
Source effect/Result.ts:12928 lines
export const orElse: {
  <E, A2, E2>(that: (err: E) => Result<A2, E2>): <A>(self: Result<A, E>) => Result<A | A2, E2>
  <A, E, A2, E2>(self: Result<A, E>, that: (err: E) => Result<A2, E2>): Result<A | A2, E2>
} = dual(
  2,
  <A, E, A2, E2>(self: Result<A, E>, that: (err: E) => Result<A2, E2>): Result<A | A2, E2> =>
    isFailure(self) ? that(self.failure) : succeed(self.success)
)