<Eff extends Effect<any, any, any>>(effects: Iterable<Eff>): Effect<
Success<Eff>,
Error<Eff>,
Services<Eff>
>Runs a sequence of effects and returns the result of the first successful one.
When to use
Use when you have prioritized fallback Effects, such as attempting
multiple APIs, reading configuration from several sources, or trying
alternative resource locations in order.
Details
This function executes the provided effects in sequence, stopping at the first success. If an effect succeeds, its result is returned immediately and no further effects in the sequence are executed.
If all effects fail, the returned effect fails with the error from the last
effect. If the collection is empty, the returned effect defects with an
Error whose message is "Received an empty collection of effects".
Example (Trying alternatives until one succeeds)
import { Effect } from "effect"
const primary = Effect.fail("primary unavailable")
const secondary = Effect.succeed("secondary result")
const tertiary = Effect.sync(() => {
throw new Error("not evaluated")
})
const program = Effect.firstSuccessOf([
primary,
secondary,
tertiary
])
console.log(Effect.runSync(program))
// Output: "secondary result"export const const firstSuccessOf: <
Eff extends Effect<any, any, any>
>(
effects: Iterable<Eff>
) => Effect<
Success<Eff>,
Error<Eff>,
Services<Eff>
>
Runs a sequence of effects and returns the result of the first successful
one.
When to use
Use when you have prioritized fallback Effects, such as attempting
multiple APIs, reading configuration from several sources, or trying
alternative resource locations in order.
Details
This function executes the provided effects in sequence, stopping at the
first success. If an effect succeeds, its result is returned immediately and
no further effects in the sequence are executed.
If all effects fail, the returned effect fails with the error from the last
effect. If the collection is empty, the returned effect defects with an
Error whose message is "Received an empty collection of effects".
Example (Trying alternatives until one succeeds)
import { Effect } from "effect"
const primary = Effect.fail("primary unavailable")
const secondary = Effect.succeed("secondary result")
const tertiary = Effect.sync(() => {
throw new Error("not evaluated")
})
const program = Effect.firstSuccessOf([
primary,
secondary,
tertiary
])
console.log(Effect.runSync(program))
// Output: "secondary result"
firstSuccessOf: <function (type parameter) Eff in <Eff extends Effect<any, any, any>>(effects: Iterable<Eff>): Effect<Success<Eff>, Error<Eff>, Services<Eff>>Eff extends interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<any, any, any>>(
effects: Iterable<Eff>effects: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) Eff in <Eff extends Effect<any, any, any>>(effects: Iterable<Eff>): Effect<Success<Eff>, Error<Eff>, Services<Eff>>Eff>
) => interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<type Success<T> = T extends Effect<
infer _A,
infer _E,
infer _R
>
? _A
: never
Extracts the success type from an Effect.
When to use
Use to derive the value produced by an existing effect when declaring
reusable type aliases, service interfaces, or function signatures.
Success<function (type parameter) Eff in <Eff extends Effect<any, any, any>>(effects: Iterable<Eff>): Effect<Success<Eff>, Error<Eff>, Services<Eff>>Eff>, type Error<T> = T extends Effect<
infer _A,
infer _E,
infer _R
>
? _E
: never
Extracts the error type from an Effect.
When to use
Use to derive the error type from an existing Effect type when declaring
helper types, wrappers, or APIs that preserve the effect's failure channel.
Details
Non-Effect inputs resolve to never.
Error<function (type parameter) Eff in <Eff extends Effect<any, any, any>>(effects: Iterable<Eff>): Effect<Success<Eff>, Error<Eff>, Services<Eff>>Eff>, type Services<T> = T extends Effect<
infer _A,
infer _E,
infer _R
>
? _R
: never
Extracts the required services type from an Effect.
When to use
Use to derive the context requirements of a generic or inferred Effect
without restating its R type parameter.
Services<function (type parameter) Eff in <Eff extends Effect<any, any, any>>(effects: Iterable<Eff>): Effect<Success<Eff>, Error<Eff>, Services<Eff>>Eff>> = import internalinternal.const firstSuccessOf: <
Eff extends Effect.Effect<any, any, any>
>(
effects: Iterable<Eff>
) => Effect.Effect<
Effect.Success<Eff>,
Effect.Error<Eff>,
Effect.Services<Eff>
>
firstSuccessOf