Hyperlinkv0.8.0-beta.28

Effect

Effect.repeatOrElseconsteffect/Effect.ts:7675
<R2, A, B, E, E2, E3, R3>(
  schedule: Schedule<B, A, E2, R2>,
  orElse: (error: E | E2, option: Option<B>) => Effect<B, E3, R3>
): <R>(self: Effect<A, E, R>) => Effect<B, E3, R | R2 | R3>
<A, E, R, R2, B, E2, E3, R3>(
  self: Effect<A, E, R>,
  schedule: Schedule<B, A, E2, R2>,
  orElse: (error: E | E2, option: Option<B>) => Effect<B, E3, R3>
): Effect<B, E3, R | R2 | R3>

Repeats an effect according to a schedule and runs a fallback effect if repetition fails before the schedule completes.

When to use

Use when successful repetitions should follow a schedule, but failures from the repeated effect or schedule need an effectful fallback.

Details

If the repeated effect or schedule step fails, orElse receives the failure and the latest schedule metadata when at least one schedule step has run; otherwise it receives None. If the schedule completes normally, the returned effect succeeds with the schedule's output.

Example (Recovering after repetition stops)

import { Console, Effect, Option, Schedule } from "effect"

let attempt = 0
const task = Effect.gen(function*() {
  attempt++
  if (attempt <= 2) {
    yield* Console.log(`Attempt ${attempt} failed`)
    return yield* Effect.fail(`Error ${attempt}`)
  }
  yield* Console.log(`Attempt ${attempt} succeeded`)
  return "success"
})

const program = Effect.repeatOrElse(
  task,
  Schedule.recurs(3),
  (error, attempts) =>
    Console.log(
      `Final failure: ${error}, after ${
        Option.getOrElse(attempts, () => 0)
      } attempts`
    ).pipe(Effect.map(() => 0))
)
repetition
Source effect/Effect.ts:767511 lines
export const repeatOrElse: {
  <R2, A, B, E, E2, E3, R3>(
    schedule: Schedule<B, A, E2, R2>,
    orElse: (error: E | E2, option: Option<B>) => Effect<B, E3, R3>
  ): <R>(self: Effect<A, E, R>) => Effect<B, E3, R | R2 | R3>
  <A, E, R, R2, B, E2, E3, R3>(
    self: Effect<A, E, R>,
    schedule: Schedule<B, A, E2, R2>,
    orElse: (error: E | E2, option: Option<B>) => Effect<B, E3, R3>
  ): Effect<B, E3, R | R2 | R3>
} = internalSchedule.repeatOrElse