Hyperlinkv0.8.0-beta.28

Effect

Effect.raceFirstconsteffect/Effect.ts:4883
<A2, E2, R2>(
  that: Effect<A2, E2, R2>,
  options?: {
    readonly onWinner?: (options: {
      readonly fiber: Fiber<any, any>
      readonly index: number
      readonly parentFiber: Fiber<any, any>
    }) => void
  }
): <A, E, R>(self: Effect<A, E, R>) => Effect<A | A2, E | E2, R | R2>
<A, E, R, A2, E2, R2>(
  self: Effect<A, E, R>,
  that: Effect<A2, E2, R2>,
  options?: {
    readonly onWinner?: (options: {
      readonly fiber: Fiber<any, any>
      readonly index: number
      readonly parentFiber: Fiber<any, any>
    }) => void
  }
): Effect<A | A2, E | E2, R | R2>

Races two effects and returns the result of the first one to complete, whether it succeeds or fails.

When to use

Use when any completion, including failure, should decide the race and interrupt the losing effect.

Details

The losing effect is interrupted, and onWinner can observe the winning fiber.

Example (Observing the winning fiber)

import { Console, Duration, Effect } from "effect"

const fastFail = Effect.delay(Effect.fail("fast-fail"), Duration.millis(10))
const slowSuccess = Effect.delay(Effect.succeed("slow-success"), Duration.millis(50))

const program = Effect.gen(function*() {
  const message = yield* Effect.match(Effect.raceFirst(fastFail, slowSuccess), {
    onFailure: (error) => `failed: ${error}`,
    onSuccess: (value) => `succeeded: ${value}`
  })
  yield* Console.log(message)
})

Effect.runPromise(program)
// Output: failed: fast-fail
racing
Source effect/Effect.ts:488319 lines
export const raceFirst: {
  <A2, E2, R2>(
    that: Effect<A2, E2, R2>,
    options?: {
      readonly onWinner?: (
        options: { readonly fiber: Fiber<any, any>; readonly index: number; readonly parentFiber: Fiber<any, any> }
      ) => void
    }
  ): <A, E, R>(self: Effect<A, E, R>) => Effect<A | A2, E | E2, R | R2>
  <A, E, R, A2, E2, R2>(
    self: Effect<A, E, R>,
    that: Effect<A2, E2, R2>,
    options?: {
      readonly onWinner?: (
        options: { readonly fiber: Fiber<any, any>; readonly index: number; readonly parentFiber: Fiber<any, any> }
      ) => void
    }
  ): Effect<A | A2, E | E2, R | R2>
} = internal.raceFirst
Referenced by 2 symbols