Hyperlinkv0.8.0-beta.28

Effect

Effect.orElseSucceedconsteffect/Effect.ts:4375
<A2>(evaluate: LazyArg<A2>): <A, E, R>(
  self: Effect<A, E, R>
) => Effect<A2 | A, never, R>
<A, E, R, A2>(self: Effect<A, E, R>, evaluate: LazyArg<A2>): Effect<
  A | A2,
  never,
  R
>

Recovers from a typed failure by producing a fallback success value.

Details

If the source effect succeeds, its value is preserved. If it fails in the error channel, orElseSucceed evaluates the fallback and succeeds with that value, removing the typed error from the returned effect.

Defects and interruptions are not recovered by this operator.

Example (Replacing failures with a value)

import { Effect } from "effect"

const validate = (age: number): Effect.Effect<number, string> => {
  if (age < 0) {
    return Effect.fail("NegativeAgeError")
  } else if (age < 18) {
    return Effect.fail("IllegalAgeError")
  } else {
    return Effect.succeed(age)
  }
}

const program = Effect.orElseSucceed(validate(-1), () => 18)

console.log(Effect.runSyncExit(program))
// Output:
// { _id: 'Exit', _tag: 'Success', value: 18 }
fallback
Source effect/Effect.ts:43759 lines
export const orElseSucceed: {
  <A2>(
    evaluate: LazyArg<A2>
  ): <A, E, R>(self: Effect<A, E, R>) => Effect<A2 | A, never, R>
  <A, E, R, A2>(
    self: Effect<A, E, R>,
    evaluate: LazyArg<A2>
  ): Effect<A | A2, never, R>
} = internal.orElseSucceed