Files
effect/src/Effect.ts
T
2025-01-11 11:44:10 +13:00

5320 lines
159 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @since 2.0.0
*/
import type * as Arr from "./Array.js"
import type { Cause, Failure, NoSuchElementError, TimeoutError } from "./Cause.js"
import type { Context, Reference, Tag } from "./Context.js"
import type { DurationInput } from "./Duration.js"
import type { Either } from "./Either.js"
import type { Exit } from "./Exit.js"
import type { Fiber } from "./Fiber.js"
import { dual, type LazyArg } from "./Function.js"
import type { TypeLambda } from "./HKT.js"
import * as core from "./internal/core.js"
import * as internalLayer from "./internal/layer.js"
import * as internalRequest from "./internal/request.js"
import * as internalSchedule from "./internal/schedule.js"
import type { Layer } from "./Layer.js"
import type { Logger } from "./Logger.js"
import type * as Option from "./Option.js"
import type { Pipeable } from "./Pipeable.js"
import { type Predicate, type Refinement } from "./Predicate.js"
import { CurrentLogAnnotations, CurrentLogSpans } from "./References.js"
import type { Request } from "./Request.js"
import type { RequestResolver } from "./RequestResolver.js"
import type { Schedule } from "./Schedule.js"
import type { Scheduler } from "./Scheduler.js"
import type { Scope } from "./Scope.js"
import type { AnySpan, ParentSpan, Span, SpanLink, SpanOptions, Tracer } from "./Tracer.js"
import type { Concurrency, Covariant, NoInfer, NotFunction } from "./Types.js"
import type * as Unify from "./Unify.js"
import type { YieldWrap } from "./Utils.js"
/**
* @since 4.0.0
* @category Symbols
*/
export const TypeId: unique symbol = Symbol.for("effect/Effect")
/**
* @since 4.0.0
* @category Symbols
*/
export type TypeId = typeof TypeId
/**
* 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`.
*
* `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.
*
* @since 2.0.0
* @category Models
*/
export interface Effect<out A, out E = never, out R = never> extends Pipeable {
readonly [TypeId]: Effect.Variance<A, E, R>
[Symbol.iterator](): EffectIterator<Effect<A, E, R>>
[Unify.typeSymbol]?: unknown
[Unify.unifySymbol]?: EffectUnify<this>
[Unify.ignoreSymbol]?: EffectUnifyIgnore
}
/**
* @category models
* @since 2.0.0
*/
export interface EffectUnify<A extends { [Unify.typeSymbol]?: any }> {
Effect?: () => A[Unify.typeSymbol] extends
| Effect<infer A0, infer E0, infer R0>
| infer _ ? Effect<A0, E0, R0>
: never
}
/**
* @category models
* @since 2.0.0
*/
export interface EffectUnifyIgnore {
Effect?: true
}
/**
* @category type lambdas
* @since 2.0.0
*/
export interface EffectTypeLambda extends TypeLambda {
readonly type: Effect<this["Target"], this["Out1"], this["Out2"]>
}
/**
* @since 2.0.0
*/
export declare namespace Effect {
/**
* @since 2.0.0
*/
export interface Variance<A, E, R> {
_A: Covariant<A>
_E: Covariant<E>
_R: Covariant<R>
}
/**
* @since 2.0.0
*/
export type Success<T> = T extends Effect<infer _A, infer _E, infer _R> ? _A : never
/**
* @since 2.0.0
*/
export type Error<T> = T extends Effect<infer _A, infer _E, infer _R> ? _E : never
/**
* @since 2.0.0
*/
export type Context<T> = T extends Effect<infer _A, infer _E, infer _R> ? _R : never
}
/**
* @since 2.0.0
* @category guards
*/
export const isEffect = (u: unknown): u is Effect<any, any, any> => typeof u === "object" && u !== null && TypeId in u
/**
* @since 2.0.0
* @category models
*/
export interface EffectIterator<T extends Effect<any, any, any>> {
next(
...args: ReadonlyArray<any>
): IteratorResult<YieldWrap<T>, Effect.Success<T>>
}
// ========================================================================
// Collecting
// ========================================================================
/**
* @since 2.0.0
*/
export declare namespace All {
/**
* @since 2.0.0
*/
export type EffectAny = Effect<any, any, any>
/**
* @since 2.0.0
*/
export type ReturnIterable<
T extends Iterable<EffectAny>,
Discard extends boolean
> = [T] extends [Iterable<Effect<infer A, infer E, infer R>>] ? Effect<Discard extends true ? void : Array<A>, E, R>
: never
/**
* @since 2.0.0
*/
export type ReturnTuple<
T extends ReadonlyArray<unknown>,
Discard extends boolean
> = Effect<
Discard extends true ? void
: T[number] extends never ? []
: {
-readonly [K in keyof T]: T[K] extends Effect<
infer _A,
infer _E,
infer _R
> ? _A
: never
},
T[number] extends never ? never
: T[number] extends Effect<infer _A, infer _E, infer _R> ? _E
: never,
T[number] extends never ? never
: T[number] extends Effect<infer _A, infer _E, infer _R> ? _R
: never
> extends infer X ? X
: never
/**
* @since 2.0.0
*/
export type ReturnObject<T, Discard extends boolean> = [T] extends [
Record<string, EffectAny>
] ? Effect<
Discard extends true ? void
: {
-readonly [K in keyof T]: [T[K]] extends [
Effect<infer _A, infer _E, infer _R>
] ? _A
: never
},
keyof T extends never ? never
: T[keyof T] extends Effect<infer _A, infer _E, infer _R> ? _E
: never,
keyof T extends never ? never
: T[keyof T] extends Effect<infer _A, infer _E, infer _R> ? _R
: never
>
: never
/**
* @since 2.0.0
*/
export type IsDiscard<A> = [Extract<A, { readonly discard: true }>] extends [
never
] ? false
: true
/**
* @since 2.0.0
*/
export type Return<
Arg extends Iterable<EffectAny> | Record<string, EffectAny>,
O extends {
readonly concurrency?: Concurrency | undefined
readonly discard?: boolean | undefined
}
> = [Arg] extends [ReadonlyArray<EffectAny>] ? ReturnTuple<Arg, IsDiscard<O>>
: [Arg] extends [Iterable<EffectAny>] ? ReturnIterable<Arg, IsDiscard<O>>
: [Arg] extends [Record<string, EffectAny>] ? ReturnObject<Arg, IsDiscard<O>>
: never
}
/**
* Combines multiple effects into one, returning results based on the input
* structure.
*
* **Details**
*
* Use this function when you need to run multiple effects and combine their
* results into a single output. It supports tuples, iterables, structs, and
* records, making it flexible for different input types.
*
* For instance, if the input is a tuple:
*
* ```ts
* // ┌─── a tuple of effects
* // ▼
* Effect.all([effect1, effect2, ...])
* ```
*
* the effects are executed sequentially, and the result is a new effect
* containing the results as a tuple. The results in the tuple match the order
* of the effects passed to `Effect.all`.
*
* **Concurrency**
*
* You can control the execution order (e.g., sequential vs. concurrent) using
* the `concurrency` option.
*
* **Short-Circuiting Behavior**
*
* This function stops execution on the first error it encounters, this is
* called "short-circuiting". If any effect in the collection fails, the
* remaining effects will not run, and the error will be propagated. To change
* this behavior, you can use the `mode` option, which allows all effects to run
* and collect results as `Either` or `Option`.
*
* **The `mode` option**
*
* The `{ mode: "either" }` option changes the behavior of `Effect.all` to
* ensure all effects run, even if some fail. Instead of stopping on the first
* failure, this mode collects both successes and failures, returning an array
* of `Either` instances where each result is either a `Right` (success) or a
* `Left` (failure).
*
* Similarly, the `{ mode: "validate" }` option uses `Option` to indicate
* success or failure. Each effect returns `None` for success and `Some` with
* the error for failure.
*
* @see {@link forEach} for iterating over elements and applying an effect.
* @see {@link allWith} for a data-last version of this function.
*
* @example
* ```ts
* // Title: Combining Effects in Tuples
* import { Effect, Console } from "effect"
*
* const tupleOfEffects = [
* Effect.succeed(42).pipe(Effect.tap(Console.log)),
* Effect.succeed("Hello").pipe(Effect.tap(Console.log))
* ] as const
*
* // ┌─── Effect<[number, string], never, never>
* // ▼
* const resultsAsTuple = Effect.all(tupleOfEffects)
*
* Effect.runPromise(resultsAsTuple).then(console.log)
* // Output:
* // 42
* // Hello
* // [ 42, 'Hello' ]
* ```
*
* @example
* // Title: Combining Effects in Iterables
* import { Effect, Console } from "effect"
*
* const iterableOfEffects: Iterable<Effect.Effect<number>> = [1, 2, 3].map(
* (n) => Effect.succeed(n).pipe(Effect.tap(Console.log))
* )
*
* // ┌─── Effect<number[], never, never>
* // ▼
* const resultsAsArray = Effect.all(iterableOfEffects)
*
* Effect.runPromise(resultsAsArray).then(console.log)
* // Output:
* // 1
* // 2
* // 3
* // [ 1, 2, 3 ]
*
* @example
* // Title: Combining Effects in Structs
* import { Effect, Console } from "effect"
*
* const structOfEffects = {
* a: Effect.succeed(42).pipe(Effect.tap(Console.log)),
* b: Effect.succeed("Hello").pipe(Effect.tap(Console.log))
* }
*
* // ┌─── Effect<{ a: number; b: string; }, never, never>
* // ▼
* const resultsAsStruct = Effect.all(structOfEffects)
*
* Effect.runPromise(resultsAsStruct).then(console.log)
* // Output:
* // 42
* // Hello
* // { a: 42, b: 'Hello' }
*
* @example
* // Title: Combining Effects in Records
* import { Effect, Console } from "effect"
*
* const recordOfEffects: Record<string, Effect.Effect<number>> = {
* key1: Effect.succeed(1).pipe(Effect.tap(Console.log)),
* key2: Effect.succeed(2).pipe(Effect.tap(Console.log))
* }
*
* // ┌─── Effect<{ [x: string]: number; }, never, never>
* // ▼
* const resultsAsRecord = Effect.all(recordOfEffects)
*
* Effect.runPromise(resultsAsRecord).then(console.log)
* // Output:
* // 1
* // 2
* // { key1: 1, key2: 2 }
*
* @example
* // Title: Short-Circuiting Behavior
* import { Effect, Console } from "effect"
*
* const program = Effect.all([
* Effect.succeed("Task1").pipe(Effect.tap(Console.log)),
* Effect.fail("Task2: Oh no!").pipe(Effect.tap(Console.log)),
* // Won't execute due to earlier failure
* Effect.succeed("Task3").pipe(Effect.tap(Console.log))
* ])
*
* Effect.runPromiseExit(program).then(console.log)
* // Output:
* // Task1
* // {
* // _id: 'Exit',
* // _tag: 'Failure',
* // cause: { _id: 'Cause', _tag: 'Fail', failure: 'Task2: Oh no!' }
* // }
*
* @example
* // Title: Collecting Results with `mode: "either"`
* import { Effect, Console } from "effect"
*
* const effects = [
* Effect.succeed("Task1").pipe(Effect.tap(Console.log)),
* Effect.fail("Task2: Oh no!").pipe(Effect.tap(Console.log)),
* Effect.succeed("Task3").pipe(Effect.tap(Console.log))
* ]
*
* const program = Effect.all(effects, { mode: "either" })
*
* Effect.runPromiseExit(program).then(console.log)
* // Output:
* // Task1
* // Task3
* // {
* // _id: 'Exit',
* // _tag: 'Success',
* // value: [
* // { _id: 'Either', _tag: 'Right', right: 'Task1' },
* // { _id: 'Either', _tag: 'Left', left: 'Task2: Oh no!' },
* // { _id: 'Either', _tag: 'Right', right: 'Task3' }
* // ]
* // }
*
* @example
* //Example: Collecting Results with `mode: "validate"`
* import { Effect, Console } from "effect"
*
* const effects = [
* Effect.succeed("Task1").pipe(Effect.tap(Console.log)),
* Effect.fail("Task2: Oh no!").pipe(Effect.tap(Console.log)),
* Effect.succeed("Task3").pipe(Effect.tap(Console.log))
* ]
*
* const program = Effect.all(effects, { mode: "validate" })
*
* Effect.runPromiseExit(program).then((result) => console.log("%o", result))
* // Output:
* // Task1
* // Task3
* // {
* // _id: 'Exit',
* // _tag: 'Failure',
* // cause: {
* // _id: 'Cause',
* // _tag: 'Fail',
* // failure: [
* // { _id: 'Option', _tag: 'None' },
* // { _id: 'Option', _tag: 'Some', value: 'Task2: Oh no!' },
* // { _id: 'Option', _tag: 'None' }
* // ]
* // }
* // }
*
* @since 2.0.0
* @category Collecting
*/
export const all: <
const Arg extends Iterable<Effect<any, any, any>> | Record<string, Effect<any, any, any>>,
O extends { readonly concurrency?: Concurrency | undefined; readonly discard?: boolean | undefined }
>(arg: Arg, options?: O) => All.Return<Arg, O> = core.all
/**
* Executes an effectful operation for each element in an `Iterable`.
*
* **Details**
*
* The `forEach` function applies a provided operation to each element in the
* iterable, producing a new effect that returns an array of results.
*
* If any effect fails, the iteration stops immediately (short-circuiting), and
* the error is propagated.
*
* **Concurrency**
*
* The `concurrency` option controls how many operations are performed
* concurrently. By default, the operations are performed sequentially.
*
* **Discarding Results**
*
* If the `discard` option is set to `true`, the intermediate results are not
* collected, and the final result of the operation is `void`.
*
* @see {@link all} for combining multiple effects into one.
*
* @example
* ```ts
* // Title: Applying Effects to Iterable Elements
* import { Effect, Console } from "effect"
*
* const result = Effect.forEach([1, 2, 3, 4, 5], (n, index) =>
* Console.log(`Currently at index ${index}`).pipe(Effect.as(n * 2))
* )
*
* Effect.runPromise(result).then(console.log)
* // Output:
* // Currently at index 0
* // Currently at index 1
* // Currently at index 2
* // Currently at index 3
* // Currently at index 4
* // [ 2, 4, 6, 8, 10 ]
* ```
*
* @example
* // Title: Using discard to Ignore Results
* import { Effect, Console } from "effect"
*
* // Apply effects but discard the results
* const result = Effect.forEach(
* [1, 2, 3, 4, 5],
* (n, index) =>
* Console.log(`Currently at index ${index}`).pipe(Effect.as(n * 2)),
* { discard: true }
* )
*
* Effect.runPromise(result).then(console.log)
* // Output:
* // Currently at index 0
* // Currently at index 1
* // Currently at index 2
* // Currently at index 3
* // Currently at index 4
* // undefined
*
* @since 2.0.0
* @category Collecting
*/
export const forEach: {
<B, E, R, S extends Iterable<any>>(
f: (a: Arr.ReadonlyArray.Infer<S>, i: number) => Effect<B, E, R>,
options?: {
readonly concurrency?: Concurrency | undefined
readonly discard?: false | undefined
} | undefined
): (
self: S
) => Effect<Arr.ReadonlyArray.With<S, B>, E, R>
<A, B, E, R>(
f: (a: A, i: number) => Effect<B, E, R>,
options: {
readonly concurrency?: Concurrency | undefined
readonly discard: true
}
): (self: Iterable<A>) => Effect<void, E, R>
<B, E, R, S extends Iterable<any>>(
self: S,
f: (a: Arr.ReadonlyArray.Infer<S>, i: number) => Effect<B, E, R>,
options?: {
readonly concurrency?: Concurrency | undefined
readonly discard?: false | undefined
} | undefined
): Effect<Arr.ReadonlyArray.With<S, B>, E, R>
<A, B, E, R>(
self: Iterable<A>,
f: (a: A, i: number) => Effect<B, E, R>,
options: {
readonly concurrency?: Concurrency | undefined
readonly discard: true
}
): Effect<void, E, R>
} = core.forEach as any
/**
* @since 2.0.0
* @category Collecting
*/
export const whileLoop: <A, E, R>(options: {
readonly while: LazyArg<boolean>
readonly body: LazyArg<Effect<A, E, R>>
readonly step: (a: A) => void
}) => Effect<void, E, R> = core.whileLoop
// -----------------------------------------------------------------------------
// Creating Effects
// -----------------------------------------------------------------------------
/**
* Creates an `Effect` that represents an asynchronous computation guaranteed to
* succeed.
*
* **When to Use**
*
* Use `promise` when you are sure the operation will not reject.
*
* **Details**
*
* The provided function (`thunk`) returns a `Promise` that should never reject; if it does, the error
* will be treated as a "defect".
*
* This defect is not a standard error but indicates a flaw in the logic that
* was expected to be error-free. You can think of it similar to an unexpected
* crash in the program, which can be further managed or logged using tools like
* {@link catchAllDefect}.
*
* **Interruptions**
*
* An optional `AbortSignal` can be provided to allow for interruption of the
* wrapped `Promise` API.
*
* @see {@link tryPromise} for a version that can handle failures.
*
* @example
* ```ts
* // Title: Delayed Message
* import { Effect } from "effect"
*
* const delay = (message: string) =>
* Effect.promise<string>(
* () =>
* new Promise((resolve) => {
* setTimeout(() => {
* resolve(message)
* }, 2000)
* })
* )
*
* // ┌─── Effect<string, never, never>
* // ▼
* const program = delay("Async operation completed successfully!")
* ```
*
* @since 2.0.0
* @category Creating Effects
*/
export const promise: <A>(
evaluate: (signal: AbortSignal) => PromiseLike<A>
) => Effect<A> = core.promise
/**
* Creates an `Effect` that always succeeds with a given value.
*
* **When to Use**
*
* Use this function when you need an effect that completes successfully with a
* specific value without any errors or external dependencies.
*
* @see {@link fail} to create an effect that represents a failure.
*
* @example
* ```ts
* // Title: Creating a Successful Effect
* import { Effect } from "effect"
*
* // Creating an effect that represents a successful scenario
* //
* // ┌─── Effect<number, never, never>
* // ▼
* const success = Effect.succeed(42)
* ```
*
* @since 2.0.0
* @category Creating Effects
*/
export const succeed: <A>(value: A) => Effect<A> = core.succeed
/**
* Returns an effect which succeeds with `None`.
*
* @since 2.0.0
* @category Creating Effects
*/
export const succeedNone: Effect<Option.Option<never>> = core.succeedNone
/**
* Returns an effect which succeeds with the value wrapped in a `Some`.
*
* @since 2.0.0
* @category Creating Effects
*/
export const succeedSome: <A>(value: A) => Effect<Option.Option<A>> = core.succeedSome
/**
* Delays the creation of an `Effect` until it is actually needed.
*
* **When to Use**
*
* Use `suspend` when you need to defer the evaluation of an effect until it is required. This is particularly useful for optimizing expensive computations, managing circular dependencies, or resolving type inference issues.
*
* **Details**
*
* `suspend` takes a thunk that represents the effect and wraps it in a suspended effect. This means the effect will not be created until it is explicitly needed, which is helpful in various scenarios:
* - **Lazy Evaluation**: Helps optimize performance by deferring computations, especially when the effect might not be needed, or when its computation is expensive. This also ensures that any side effects or scoped captures are re-executed on each invocation.
* - **Handling Circular Dependencies**: Useful in managing circular dependencies, such as recursive functions that need to avoid eager evaluation to prevent stack overflow.
* - **Unifying Return Types**: Can help TypeScript unify return types in situations where multiple branches of logic return different effects, simplifying type inference.
*
* @example
* ```ts
* // Title: Lazy Evaluation with Side Effects
* import { Effect } from "effect"
*
* let i = 0
*
* const bad = Effect.succeed(i++)
*
* const good = Effect.suspend(() => Effect.succeed(i++))
*
* console.log(Effect.runSync(bad)) // Output: 0
* console.log(Effect.runSync(bad)) // Output: 0
*
* console.log(Effect.runSync(good)) // Output: 1
* console.log(Effect.runSync(good)) // Output: 2
* ```
*
* @example
* // Title: Recursive Fibonacci
* import { Effect } from "effect"
*
* const blowsUp = (n: number): Effect.Effect<number> =>
* n < 2
* ? Effect.succeed(1)
* : Effect.zipWith(blowsUp(n - 1), blowsUp(n - 2), (a, b) => a + b)
*
* // console.log(Effect.runSync(blowsUp(32)))
* // crash: JavaScript heap out of memory
*
* const allGood = (n: number): Effect.Effect<number> =>
* n < 2
* ? Effect.succeed(1)
* : Effect.zipWith(
* Effect.suspend(() => allGood(n - 1)),
* Effect.suspend(() => allGood(n - 2)),
* (a, b) => a + b
* )
*
* console.log(Effect.runSync(allGood(32)))
* // Output: 3524578
*
* @example
* // Title: Using Effect.suspend to Help TypeScript Infer Types
* import { Effect } from "effect"
*
* // Without suspend, TypeScript may struggle with type inference.
* // Inferred type:
* // (a: number, b: number) =>
* // Effect<never, Error, never> | Effect<number, never, never>
* const withoutSuspend = (a: number, b: number) =>
* b === 0
* ? Effect.fail(new Error("Cannot divide by zero"))
* : Effect.succeed(a / b)
*
* // Using suspend to unify return types.
* // Inferred type:
* // (a: number, b: number) => Effect<number, Error, never>
* const withSuspend = (a: number, b: number) =>
* Effect.suspend(() =>
* b === 0
* ? Effect.fail(new Error("Cannot divide by zero"))
* : Effect.succeed(a / b)
* )
*
* @since 2.0.0
* @category Creating Effects
*/
export const suspend: <A, E, R>(
effect: LazyArg<Effect<A, E, R>>
) => Effect<A, E, R> = core.suspend
/**
* Creates an `Effect` that represents a synchronous side-effectful computation.
*
* **When to Use**
*
* Use `sync` when you are sure the operation will not fail.
*
* **Details**
*
* The provided function (`thunk`) must not throw errors; if it does, the error
* will be treated as a "defect".
*
* This defect is not a standard error but indicates a flaw in the logic that
* was expected to be error-free. You can think of it similar to an unexpected
* crash in the program, which can be further managed or logged using tools like
* {@link catchAllDefect}.
*
* @see {@link try_ | try} for a version that can handle failures.
*
* @example
* ```ts
* // Title: Logging a Message
* import { Effect } from "effect"
*
* const log = (message: string) =>
* Effect.sync(() => {
* console.log(message) // side effect
* })
*
* // ┌─── Effect<void, never, never>
* // ▼
* const program = log("Hello, World!")
* ```
*
* @since 2.0.0
* @category Creating Effects
*/
export const sync: <A>(thunk: LazyArg<A>) => Effect<A> = core.sync
const _void: Effect<void> = core.void
export {
/**
* @since 2.0.0
* @category Creating Effects
*/
_void as void
}
/**
* Creates an `Effect` from a callback-based asynchronous function.
*
* **Details**
*
* The `resume` function:
* - Must be called exactly once. Any additional calls will be ignored.
* - Can return an optional `Effect` that will be run if the `Fiber` executing
* this `Effect` is interrupted. This can be useful in scenarios where you
* need to handle resource cleanup if the operation is interrupted.
* - Can receive an `AbortSignal` to handle interruption if needed.
*
* The `FiberId` of the fiber that may complete the async callback may also be
* specified using the `blockingOn` argument. This is called the "blocking
* fiber" because it suspends the fiber executing the `async` effect (i.e.
* semantically blocks the fiber from making progress). Specifying this fiber id
* in cases where it is known will improve diagnostics, but not affect the
* behavior of the returned effect.
*
* **When to Use**
*
* Use `Effect.async` when dealing with APIs that use callback-style instead of
* `async/await` or `Promise`.
*
* @example
* ```ts
* // Title: Wrapping a Callback API
* import { Effect } from "effect"
* import * as NodeFS from "node:fs"
*
* const readFile = (filename: string) =>
* Effect.async<Buffer, Error>((resume) => {
* NodeFS.readFile(filename, (error, data) => {
* if (error) {
* // Resume with a failed Effect if an error occurs
* resume(Effect.fail(error))
* } else {
* // Resume with a succeeded Effect if successful
* resume(Effect.succeed(data))
* }
* })
* })
*
* // ┌─── Effect<Buffer, Error, never>
* // ▼
* const program = readFile("example.txt")
* ```
*
* @example
* // Title: Handling Interruption with Cleanup
* import { Effect, Fiber } from "effect"
* import * as NodeFS from "node:fs"
*
* // Simulates a long-running operation to write to a file
* const writeFileWithCleanup = (filename: string, data: string) =>
* Effect.async<void, Error>((resume) => {
* const writeStream = NodeFS.createWriteStream(filename)
*
* // Start writing data to the file
* writeStream.write(data)
*
* // When the stream is finished, resume with success
* writeStream.on("finish", () => resume(Effect.void))
*
* // In case of an error during writing, resume with failure
* writeStream.on("error", (err) => resume(Effect.fail(err)))
*
* // Handle interruption by returning a cleanup effect
* return Effect.sync(() => {
* console.log(`Cleaning up ${filename}`)
* NodeFS.unlinkSync(filename)
* })
* })
*
* const program = Effect.gen(function* () {
* const fiber = yield* Effect.fork(
* writeFileWithCleanup("example.txt", "Some long data...")
* )
* // Simulate interrupting the fiber after 1 second
* yield* Effect.sleep("1 second")
* yield* Fiber.interrupt(fiber) // This will trigger the cleanup
* })
*
* // Run the program
* Effect.runPromise(program)
* // Output:
* // Cleaning up example.txt
*
* @example
* // Title: Handling Interruption with AbortSignal
* import { Effect, Fiber } from "effect"
*
* // A task that supports interruption using AbortSignal
* const interruptibleTask = Effect.async<void, Error>((resume, signal) => {
* // Handle interruption
* signal.addEventListener("abort", () => {
* console.log("Abort signal received")
* clearTimeout(timeoutId)
* })
*
* // Simulate a long-running task
* const timeoutId = setTimeout(() => {
* console.log("Operation completed")
* resume(Effect.void)
* }, 2000)
* })
*
* const program = Effect.gen(function* () {
* const fiber = yield* Effect.fork(interruptibleTask)
* // Simulate interrupting the fiber after 1 second
* yield* Effect.sleep("1 second")
* yield* Fiber.interrupt(fiber)
* })
*
* // Run the program
* Effect.runPromise(program)
* // Output:
* // Abort signal received
*
* @since 2.0.0
* @category Creating Effects
*/
export const async: <A, E = never, R = never>(
register: (resume: (effect: Effect<A, E, R>) => void, signal: AbortSignal) => void | Effect<void, never, R>
) => Effect<A, E, R> = core.async
/**
* Returns an effect that will never produce anything. The moral equivalent of
* `while(true) {}`, only without the wasted CPU cycles.
*
* @since 2.0.0
* @category Creating Effects
*/
export const never: Effect<never> = core.never
/**
* Provides a way to write effectful code using generator functions, simplifying
* control flow and error handling.
*
* **When to Use**
*
* `gen` allows you to write code that looks and behaves like synchronous
* code, but it can handle asynchronous tasks, errors, and complex control flow
* (like loops and conditions). It helps make asynchronous code more readable
* and easier to manage.
*
* The generator functions work similarly to `async/await` but with more
* explicit control over the execution of effects. You can `yield*` values from
* effects and return the final result at the end.
*
* @example
* ```ts
* import { Effect } from "effect"
*
* const addServiceCharge = (amount: number) => amount + 1
*
* const applyDiscount = (
* total: number,
* discountRate: number
* ): Effect.Effect<number, Error> =>
* discountRate === 0
* ? Effect.fail(new Error("Discount rate cannot be zero"))
* : Effect.succeed(total - (total * discountRate) / 100)
*
* const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
*
* const fetchDiscountRate = Effect.promise(() => Promise.resolve(5))
*
* export const program = Effect.gen(function* () {
* const transactionAmount = yield* fetchTransactionAmount
* const discountRate = yield* fetchDiscountRate
* const discountedAmount = yield* applyDiscount(
* transactionAmount,
* discountRate
* )
* const finalAmount = addServiceCharge(discountedAmount)
* return `Final amount to charge: ${finalAmount}`
* })
* ```
*
* @since 2.0.0
* @category Creating Effects
*/
export const gen: {
<Eff extends YieldWrap<Effect<any, any, any>>, AEff>(
f: () => Generator<Eff, AEff, never>
): Effect<
AEff,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer E, infer _R>>] ? E
: never,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer _E, infer R>>] ? R
: never
>
<Self, Eff extends YieldWrap<Effect<any, any, any>>, AEff>(
self: Self,
f: (this: Self) => Generator<Eff, AEff, never>
): Effect<
AEff,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer E, infer _R>>] ? E
: never,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer _E, infer R>>] ? R
: never
>
} = core.gen
/**
* Creates an `Effect` that represents a recoverable error.
*
* **When to Use**
*
* Use this function to explicitly signal an error in an `Effect`. The error
* will keep propagating unless it is handled. You can handle the error with
* functions like {@link catchAll} or {@link catchTag}.
*
* @see {@link succeed} to create an effect that represents a successful value.
*
* @example
* ```ts
* // Title: Creating a Failed Effect
* import { Effect } from "effect"
*
* // ┌─── Effect<never, Error, never>
* // ▼
* const failure = Effect.fail(
* new Error("Operation failed due to network error")
* )
* ```
*
* @since 2.0.0
* @category Creating Effects
*/
export const fail: <E>(error: E) => Effect<never, E> = core.fail
/**
* @since 2.0.0
* @category Creating Effects
*/
export const failSync: <E>(evaluate: LazyArg<E>) => Effect<never, E> = core.failSync
/**
* @since 2.0.0
* @category Creating Effects
*/
export const failCause: <E>(cause: Cause<E>) => Effect<never, E> = core.failCause
/**
* @since 2.0.0
* @category Creating Effects
*/
export const failCauseSync: <E>(
evaluate: LazyArg<Cause<E>>
) => Effect<never, E> = core.failCauseSync
/**
* Creates an effect that terminates a fiber with a specified error.
*
* **When to Use**
*
* Use `die` when encountering unexpected conditions in your code that should
* not be handled as regular errors but instead represent unrecoverable defects.
*
* **Details**
*
* The `die` function is used to signal a defect, which represents a critical
* and unexpected error in the code. When invoked, it produces an effect that
* does not handle the error and instead terminates the fiber.
*
* The error channel of the resulting effect is of type `never`, indicating that
* it cannot recover from this failure.
*
* @see {@link dieSync} for a variant that throws a specified error, evaluated lazily.
* @see {@link dieMessage} for a variant that throws a `RuntimeException` with a message.
*
* @example
* ```ts
* // Title: Terminating on Division by Zero with a Specified Error
* import { Effect } from "effect"
*
* const divide = (a: number, b: number) =>
* b === 0
* ? Effect.die(new Error("Cannot divide by zero"))
* : Effect.succeed(a / b)
*
* // ┌─── Effect<number, never, never>
* // ▼
* const program = divide(1, 0)
*
* Effect.runPromise(program).catch(console.error)
* // Output:
* // (FiberFailure) Error: Cannot divide by zero
* // ...stack trace...
* ```
*
* @since 2.0.0
* @category Creating Effects
*/
export const die: (defect: unknown) => Effect<never> = core.die
const try_: <A, E>(options: { try: LazyArg<A>; catch: (error: unknown) => E }) => Effect<A, E> = core.try
export {
/**
* Creates an `Effect` that represents a synchronous computation that might
* fail.
*
* **When to Use**
*
* In situations where you need to perform synchronous operations that might
* fail, such as parsing JSON, you can use the `try` constructor. This
* constructor is designed to handle operations that could throw exceptions by
* capturing those exceptions and transforming them into manageable errors.
*
* **Error Handling**
*
* There are two ways to handle errors with `try`:
*
* 1. If you don't provide a `catch` function, the error is caught and the
* effect fails with an `UnknownException`.
* 2. If you provide a `catch` function, the error is caught and the `catch`
* function maps it to an error of type `E`.
*
* @see {@link sync} if the effectful computation is synchronous and does not
* throw errors.
*
* @example
* ```ts
* // Title: Safe JSON Parsing
* import { Effect } from "effect"
*
* const parse = (input: string) =>
* // This might throw an error if input is not valid JSON
* Effect.try(() => JSON.parse(input))
*
* // ┌─── Effect<any, UnknownException, never>
* // ▼
* const program = parse("")
*
* ```
* @example
* // Title: Custom Error Handling
* import { Effect } from "effect"
*
* const parse = (input: string) =>
* Effect.try({
* // JSON.parse may throw for bad input
* try: () => JSON.parse(input),
* // remap the error
* catch: (unknown) => new Error(`something went wrong ${unknown}`)
* })
*
* // ┌─── Effect<any, Error, never>
* // ▼
* const program = parse("")
*
* @since 2.0.0
* @category Creating Effects
*/
try_ as try
}
/**
* @since 4.0.0
* @category Creating Effects
*/
export const fromOption: <A>(option: Option.Option<A>) => Effect<A, core.NoSuchElementError> = core.fromOption
/**
* @since 4.0.0
* @category Creating Effects
*/
export const fromEither: <R, L>(either: Either<R, L>) => Effect<R, L> = core.fromEither
/**
* @since 2.0.0
* @category Creating Effects
*/
export const yieldNow: Effect<void> = core.yieldNow
/**
* @since 2.0.0
* @category Creating Effects
*/
export const withFiber: <A, E = never, R = never>(
evaluate: (fiber: Fiber<A, E>) => Effect<A, E, R>
) => Effect<A, E, R> = core.withFiber
/**
* @since 4.0.0
* @category Creating Effects
*/
export const withFiberUnknown: <A, E, R>(
evaluate: (fiber: Fiber<unknown, unknown>) => Effect<A, E, R>
) => Effect<A, E, R> = core.withFiberUnknown
// -----------------------------------------------------------------------------
// Mapping
// -----------------------------------------------------------------------------
/**
* Chains effects to produce new `Effect` instances, useful for combining
* operations that depend on previous results.
*
* **Syntax**
* ```ts
* const flatMappedEffect = pipe(myEffect, Effect.flatMap(transformation))
* // or
* const flatMappedEffect = Effect.flatMap(myEffect, transformation)
* // or
* const flatMappedEffect = myEffect.pipe(Effect.flatMap(transformation))
* ```
*
* **When to Use**
*
* Use `flatMap` when you need to chain multiple effects, ensuring that each
* step produces a new `Effect` while flattening any nested effects that may
* occur.
*
* **Details**
*
* `flatMap` lets you sequence effects so that the result of one effect can be
* used in the next step. It is similar to `flatMap` used with arrays but works
* specifically with `Effect` instances, allowing you to avoid deeply nested
* effect structures.
*
* Since effects are immutable, `flatMap` always returns a new effect instead of
* changing the original one.
*
* @example
* ```ts
* import { pipe, Effect } from "effect"
*
* // Function to apply a discount safely to a transaction amount
* const applyDiscount = (
* total: number,
* discountRate: number
* ): Effect.Effect<number, Error> =>
* discountRate === 0
* ? Effect.fail(new Error("Discount rate cannot be zero"))
* : Effect.succeed(total - (total * discountRate) / 100)
*
* // Simulated asynchronous task to fetch a transaction amount from database
* const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
*
* // Chaining the fetch and discount application using `flatMap`
* const finalAmount = pipe(
* fetchTransactionAmount,
* Effect.flatMap((amount) => applyDiscount(amount, 5))
* )
*
* Effect.runPromise(finalAmount).then(console.log)
* // Output: 95
* ```
*
* @since 2.0.0
* @category Mapping
*/
export const flatMap: {
<A, B, E1, R1>(
f: (a: A) => Effect<B, E1, R1>
): <E, R>(self: Effect<A, E, R>) => Effect<B, E1 | E, R1 | R>
<A, E, R, B, E1, R1>(
self: Effect<A, E, R>,
f: (a: A) => Effect<B, E1, R1>
): Effect<B, E | E1, R | R1>
} = core.flatMap
/**
* Chains two actions, where the second action can depend on the result of the
* first.
*
* **Syntax**
* ```ts
* const transformedEffect = pipe(myEffect, Effect.andThen(anotherEffect))
* // or
* const transformedEffect = Effect.andThen(myEffect, anotherEffect)
* // or
* const transformedEffect = myEffect.pipe(Effect.andThen(anotherEffect))
* ```
*
* **When to Use**
*
* Use `andThen` when you need to run multiple actions in sequence, with the
* second action depending on the result of the first. This is useful for
* combining effects or handling computations that must happen in order.
*
* **Details**
*
* The second action can be:
*
* - A constant value (similar to {@link as})
* - A function returning a value (similar to {@link map})
* - An `Effect`
* - A function returning an `Effect` (similar to {@link flatMap})
*
* **Note:** `andThen` works well with both `Option` and `Either` types,
* treating them as effects.
*
* @example
* ```ts
* // Title: Applying a Discount Based on Fetched Amount
* import { pipe, Effect } from "effect"
*
* // Function to apply a discount safely to a transaction amount
* const applyDiscount = (
* total: number,
* discountRate: number
* ): Effect.Effect<number, Error> =>
* discountRate === 0
* ? Effect.fail(new Error("Discount rate cannot be zero"))
* : Effect.succeed(total - (total * discountRate) / 100)
*
* // Simulated asynchronous task to fetch a transaction amount from database
* const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
*
* // Using Effect.map and Effect.flatMap
* const result1 = pipe(
* fetchTransactionAmount,
* Effect.map((amount) => amount * 2),
* Effect.flatMap((amount) => applyDiscount(amount, 5))
* )
*
* Effect.runPromise(result1).then(console.log)
* // Output: 190
*
* // Using Effect.andThen
* const result2 = pipe(
* fetchTransactionAmount,
* Effect.andThen((amount) => amount * 2),
* Effect.andThen((amount) => applyDiscount(amount, 5))
* )
*
* Effect.runPromise(result2).then(console.log)
* // Output: 190
* ```
*
* @since 2.0.0
* @category Mapping
*/
export const andThen: {
<A, X>(
f: (a: A) => X
): <E, R>(
self: Effect<A, E, R>
) => [X] extends [Effect<infer A1, infer E1, infer R1>] ? Effect<A1, E | E1, R | R1>
: Effect<X, E, R>
<X>(
f: NotFunction<X>
): <A, E, R>(
self: Effect<A, E, R>
) => [X] extends [Effect<infer A1, infer E1, infer R1>] ? Effect<A1, E | E1, R | R1>
: Effect<X, E, R>
<A, E, R, X>(
self: Effect<A, E, R>,
f: (a: A) => X
): [X] extends [Effect<infer A1, infer E1, infer R1>] ? Effect<A1, E | E1, R | R1>
: Effect<X, E, R>
<A, E, R, X>(
self: Effect<A, E, R>,
f: NotFunction<X>
): [X] extends [Effect<infer A1, infer E1, infer R1>] ? Effect<A1, E | E1, R | R1>
: Effect<X, E, R>
} = core.andThen
/**
* Runs a side effect with the result of an effect without changing the original
* value.
*
* **When to Use**
*
* Use `tap` when you want to perform a side effect, like logging or tracking,
* without modifying the main value. This is useful when you need to observe or
* record an action but want the original value to be passed to the next step.
*
* **Details**
*
* `tap` works similarly to `flatMap`, but it ignores the result of the function
* passed to it. The value from the previous effect remains available for the
* next part of the chain. Note that if the side effect fails, the entire chain
* will fail too.
*
* @example
* ```ts
* // Title: Logging a step in a pipeline
* import { Console, Effect, pipe } from "effect"
*
* // Function to apply a discount safely to a transaction amount
* const applyDiscount = (
* total: number,
* discountRate: number
* ): Effect.Effect<number, Error> =>
* discountRate === 0
* ? Effect.fail(new Error("Discount rate cannot be zero"))
* : Effect.succeed(total - (total * discountRate) / 100)
*
* // Simulated asynchronous task to fetch a transaction amount from database
* const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
*
* const finalAmount = pipe(
* fetchTransactionAmount,
* // Log the fetched transaction amount
* Effect.tap((amount) => Console.log(`Apply a discount to: ${amount}`)),
* // `amount` is still available!
* Effect.flatMap((amount) => applyDiscount(amount, 5))
* )
*
* Effect.runPromise(finalAmount).then(console.log)
* // Output:
* // Apply a discount to: 100
* // 95
* ```
*
* @since 2.0.0
* @category sequencing
*/
export const tap: {
<A, X>(
f: (a: NoInfer<A>) => X
): <E, R>(
self: Effect<A, E, R>
) => [X] extends [Effect<infer _A1, infer E1, infer R1>] ? Effect<A, E | E1, R | R1>
: Effect<A, E, R>
<A, X, E1, R1>(
f: (a: NoInfer<A>) => Effect<X, E1, R1>,
options: { onlyEffect: true }
): <E, R>(self: Effect<A, E, R>) => Effect<A, E | E1, R | R1>
<X>(
f: NotFunction<X>
): <A, E, R>(
self: Effect<A, E, R>
) => [X] extends [Effect<infer _A1, infer E1, infer R1>] ? Effect<A, E | E1, R | R1>
: Effect<A, E, R>
<X, E1, R1>(
f: Effect<X, E1, R1>,
options: { onlyEffect: true }
): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E | E1, R | R1>
<A, E, R, X>(
self: Effect<A, E, R>,
f: (a: NoInfer<A>) => X
): [X] extends [Effect<infer _A1, infer E1, infer R1>] ? Effect<A, E | E1, R | R1>
: Effect<A, E, R>
<A, E, R, X, E1, R1>(
self: Effect<A, E, R>,
f: (a: NoInfer<A>) => Effect<X, E1, R1>,
options: { onlyEffect: true }
): Effect<A, E | E1, R | R1>
<A, E, R, X>(
self: Effect<A, E, R>,
f: NotFunction<X>
): [X] extends [Effect<infer _A1, infer E1, infer R1>] ? Effect<A, E | E1, R | R1>
: Effect<A, E, R>
<A, E, R, X, E1, R1>(
self: Effect<A, E, R>,
f: Effect<X, E1, R1>,
options: { onlyEffect: true }
): Effect<A, E | E1, R | R1>
} = core.tap
/**
* Transforms an effect to encapsulate both failure and success using the `Exit`
* data type.
*
* **Details**
*
* `exit` wraps an effect's success or failure inside an `Exit` type, allowing
* you to handle both cases explicitly.
*
* The resulting effect cannot fail because the failure is encapsulated within
* the `Exit.Failure` type. The error type is set to `never`, indicating that
* the effect is structured to never fail directly.
*
* @see {@link option} for a version that uses `Option` instead.
* @see {@link either} for a version that uses `Either` instead.
*
* @example
* ```ts
* import { Effect, Cause, Console, Exit } from "effect"
*
* // Simulating a runtime error
* const task = Effect.dieMessage("Boom!")
*
* const program = Effect.gen(function* () {
* const exit = yield* Effect.exit(task)
* if (Exit.isFailure(exit)) {
* const cause = exit.cause
* if (
* Cause.isDieType(cause) &&
* Cause.isRuntimeException(cause.defect)
* ) {
* yield* Console.log(
* `RuntimeException defect caught: ${cause.defect.message}`
* )
* } else {
* yield* Console.log("Unknown failure caught.")
* }
* }
* })
*
* // We get an Exit.Success because we caught all failures
* Effect.runPromiseExit(program).then(console.log)
* // Output:
* // RuntimeException defect caught: Boom!
* // {
* // _id: "Exit",
* // _tag: "Success",
* // value: undefined
* // }
* ```
*
* @since 2.0.0
* @category Outcome Encapsulation
*/
export const exit: <A, E, R>(self: Effect<A, E, R>) => Effect<Exit<A, E>, never, R> = core.exit
/**
* Transforms the value inside an effect by applying a function to it.
*
* **Syntax**
*
* ```ts
* const mappedEffect = pipe(myEffect, Effect.map(transformation))
* // or
* const mappedEffect = Effect.map(myEffect, transformation)
* // or
* const mappedEffect = myEffect.pipe(Effect.map(transformation))
* ```
*
* **Details**
*
* `map` takes a function and applies it to the value contained within an
* effect, creating a new effect with the transformed value.
*
* It's important to note that effects are immutable, meaning that the original
* effect is not modified. Instead, a new effect is returned with the updated
* value.
*
* @see {@link mapError} for a version that operates on the error channel.
* @see {@link mapBoth} for a version that operates on both channels.
* @see {@link flatMap} or {@link andThen} for a version that can return a new effect.
*
* @example
* ```ts
* // Title: Adding a Service Charge
* import { pipe, Effect } from "effect"
*
* const addServiceCharge = (amount: number) => amount + 1
*
* const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
*
* const finalAmount = pipe(
* fetchTransactionAmount,
* Effect.map(addServiceCharge)
* )
*
* Effect.runPromise(finalAmount).then(console.log)
* // Output: 101
* ```
* @since 2.0.0
* @category Mapping
*/
export const map: {
<A, B>(f: (a: A) => B): <E, R>(self: Effect<A, E, R>) => Effect<B, E, R>
<A, E, R, B>(self: Effect<A, E, R>, f: (a: A) => B): Effect<B, E, R>
} = core.map
/**
* Replaces the value inside an effect with a constant value.
*
* `as` allows you to ignore the original value inside an effect and
* replace it with a new constant value.
*
* @example
* ```ts
* // Title: Replacing a Value
* import { pipe, Effect } from "effect"
*
* // Replaces the value 5 with the constant "new value"
* const program = pipe(Effect.succeed(5), Effect.as("new value"))
*
* Effect.runPromise(program).then(console.log)
* // Output: "new value"
* ```
*
* @since 2.0.0
* @category Mapping
*/
export const as: {
<B>(value: B): <A, E, R>(self: Effect<A, E, R>) => Effect<B, E, R>
<A, E, R, B>(self: Effect<A, E, R>, value: B): Effect<B, E, R>
} = core.as
/**
* This function maps the success value of an `Effect` value to `void`. If the
* original `Effect` value succeeds, the returned `Effect` value will also
* succeed. If the original `Effect` value fails, the returned `Effect` value
* will fail with the same error.
*
* @since 2.0.0
* @category Mapping
*/
export const asVoid: <A, E, R>(self: Effect<A, E, R>) => Effect<void, E, R> = core.asVoid
/**
* The `flip` function swaps the success and error channels of an effect,
* so that the success becomes the error, and the error becomes the success.
*
* This function is useful when you need to reverse the flow of an effect,
* treating the previously successful values as errors and vice versa. This can
* be helpful in scenarios where you want to handle a success as a failure or
* treat an error as a valid result.
*
* @example
* ```ts
* import { Effect } from "effect"
*
* // ┌─── Effect<number, string, never>
* // ▼
* const program = Effect.fail("Oh uh!").pipe(Effect.as(2))
*
* // ┌─── Effect<string, number, never>
* // ▼
* const flipped = Effect.flip(program)
* ```
*
* @since 2.0.0
* @category Mapping
*/
export const flip: <A, E, R>(self: Effect<A, E, R>) => Effect<E, A, R> = core.flip
// -----------------------------------------------------------------------------
// Zipping
// -----------------------------------------------------------------------------
/**
* Combines two effects into a single effect, producing a tuple with the results of both effects.
*
* The `zip` function executes the first effect (left) and then the second effect (right).
* Once both effects succeed, their results are combined into a tuple.
*
* **Concurrency**
*
* By default, `zip` processes the effects sequentially. To execute the effects concurrently,
* use the `{ concurrent: true }` option.
*
* @see {@link zipWith} for a version that combines the results with a custom function.
* @see {@link validate} for a version that accumulates errors.
*
* @example
* ```ts
* // Title: Combining Two Effects Sequentially
* import { Effect } from "effect"
*
* const task1 = Effect.succeed(1).pipe(
* Effect.delay("200 millis"),
* Effect.tap(Effect.log("task1 done"))
* )
* const task2 = Effect.succeed("hello").pipe(
* Effect.delay("100 millis"),
* Effect.tap(Effect.log("task2 done"))
* )
*
* // Combine the two effects together
* //
* // ┌─── Effect<[number, string], never, never>
* // ▼
* const program = Effect.zip(task1, task2)
*
* Effect.runPromise(program).then(console.log)
* // Output:
* // timestamp=... level=INFO fiber=#0 message="task1 done"
* // timestamp=... level=INFO fiber=#0 message="task2 done"
* // [ 1, 'hello' ]
* ```
*
* @example
* // Title: Combining Two Effects Concurrently
* import { Effect } from "effect"
*
* const task1 = Effect.succeed(1).pipe(
* Effect.delay("200 millis"),
* Effect.tap(Effect.log("task1 done"))
* )
* const task2 = Effect.succeed("hello").pipe(
* Effect.delay("100 millis"),
* Effect.tap(Effect.log("task2 done"))
* )
*
* // Run both effects concurrently using the concurrent option
* const program = Effect.zip(task1, task2, { concurrent: true })
*
* Effect.runPromise(program).then(console.log)
* // Output:
* // timestamp=... level=INFO fiber=#0 message="task2 done"
* // timestamp=... level=INFO fiber=#0 message="task1 done"
* // [ 1, 'hello' ]
*
* @since 2.0.0
* @category Zipping
*/
export const zip: {
<A2, E2, R2>(
that: Effect<A2, E2, R2>,
options?: { readonly concurrent?: boolean | undefined } | undefined
): <A, E, R>(self: Effect<A, E, R>) => Effect<[A, A2], E2 | E, R2 | R>
<A, E, R, A2, E2, R2>(
self: Effect<A, E, R>,
that: Effect<A2, E2, R2>,
options?: { readonly concurrent?: boolean | undefined }
): Effect<[A, A2], E | E2, R | R2>
} = core.zip
/**
* Combines two effects sequentially and applies a function to their results to
* produce a single value.
*
* **When to Use**
*
* The `zipWith` function is similar to {@link zip}, but instead of returning a
* tuple of results, it applies a provided function to the results of the two
* effects, combining them into a single value.
*
* **Concurrency**
*
* By default, the effects are run sequentially. To execute them concurrently,
* use the `{ concurrent: true }` option.
*
* @example
* ```ts
* // Title: Combining Effects with a Custom Function
* import { Effect } from "effect"
*
* const task1 = Effect.succeed(1).pipe(
* Effect.delay("200 millis"),
* Effect.tap(Effect.log("task1 done"))
* )
* const task2 = Effect.succeed("hello").pipe(
* Effect.delay("100 millis"),
* Effect.tap(Effect.log("task2 done"))
* )
*
* const task3 = Effect.zipWith(
* task1,
* task2,
* // Combines results into a single value
* (number, string) => number + string.length
* )
*
* Effect.runPromise(task3).then(console.log)
* // Output:
* // timestamp=... level=INFO fiber=#3 message="task1 done"
* // timestamp=... level=INFO fiber=#2 message="task2 done"
* // 6
* ```
*
* @since 2.0.0
* @category Zipping
*/
export const zipWith: {
<A2, E2, R2, A, B>(
that: Effect<A2, E2, R2>,
f: (a: A, b: A2) => B,
options?: { readonly concurrent?: boolean | undefined }
): <E, R>(self: Effect<A, E, R>) => Effect<B, E2 | E, R2 | R>
<A, E, R, A2, E2, R2, B>(
self: Effect<A, E, R>,
that: Effect<A2, E2, R2>,
f: (a: A, b: A2) => B,
options?: { readonly concurrent?: boolean | undefined }
): Effect<B, E2 | E, R2 | R>
} = core.zipWith
// -----------------------------------------------------------------------------
// Error handling
// -----------------------------------------------------------------------------
const catch_: {
<E, A2, E2, R2>(
f: (e: E) => Effect<A2, E2, R2>
): <A, R>(self: Effect<A, E, R>) => Effect<A2 | A, E2, R2 | R>
<A, E, R, A2, E2, R2>(
self: Effect<A, E, R>,
f: (e: E) => Effect<A2, E2, R2>
): Effect<A2 | A, E2, R2 | R>
} = core.catch_
export {
/**
* Handles all errors in an effect by providing a fallback effect.
*
* **Details**
*
* The `catchAll` function catches any errors that may occur during the
* execution of an effect and allows you to handle them by specifying a fallback
* effect. This ensures that the program continues without failing by recovering
* from errors using the provided fallback logic.
*
* **Note**: `catchAll` only handles recoverable errors. It will not recover
* from unrecoverable defects.
*
* @see {@link catchAllCause} for a version that can recover from both recoverable and unrecoverable errors.
*
* @example
* ```ts
* // Title: Providing Recovery Logic for Recoverable Errors
* import { Effect, Random } from "effect"
*
* class HttpError {
* readonly _tag = "HttpError"
* }
*
* class ValidationError {
* readonly _tag = "ValidationError"
* }
*
* // ┌─── Effect<string, HttpError | ValidationError, never>
* // ▼
* const program = Effect.gen(function* () {
* const n1 = yield* Random.next
* const n2 = yield* Random.next
* if (n1 < 0.5) {
* yield* Effect.fail(new HttpError())
* }
* if (n2 < 0.5) {
* yield* Effect.fail(new ValidationError())
* }
* return "some result"
* })
*
* // ┌─── Effect<string, never, never>
* // ▼
* const recovered = program.pipe(
* Effect.catchAll((error) =>
* Effect.succeed(`Recovering from ${error._tag}`)
* )
* )
* ```
*
* @since 4.0.0
* @category Error handling
*/
catch_ as catch
}
/**
* Catches and handles specific errors by their `_tag` field, which is used as a
* discriminator.
*
* **When to Use**
*
* `catchTag` is useful when your errors are tagged with a readonly `_tag` field
* that identifies the error type. You can use this function to handle specific
* error types by matching the `_tag` value. This allows for precise error
* handling, ensuring that only specific errors are caught and handled.
*
* The error type must have a readonly `_tag` field to use `catchTag`. This
* field is used to identify and match errors.
*
* @see {@link catchTags} for a version that allows you to handle multiple error
* types at once.
*
* @example
* ```ts
* // Title: Handling Errors by Tag
* import { Effect, Random } from "effect"
*
* class HttpError {
* readonly _tag = "HttpError"
* }
*
* class ValidationError {
* readonly _tag = "ValidationError"
* }
*
* // ┌─── Effect<string, HttpError | ValidationError, never>
* // ▼
* const program = Effect.gen(function* () {
* const n1 = yield* Random.next
* const n2 = yield* Random.next
* if (n1 < 0.5) {
* yield* Effect.fail(new HttpError())
* }
* if (n2 < 0.5) {
* yield* Effect.fail(new ValidationError())
* }
* return "some result"
* })
*
* // ┌─── Effect<string, ValidationError, never>
* // ▼
* const recovered = program.pipe(
* // Only handle HttpError errors
* Effect.catchTag("HttpError", (_HttpError) =>
* Effect.succeed("Recovering from HttpError")
* )
* )
* ```
*
* @since 2.0.0
* @category Error handling
*/
export const catchTag: {
<K extends E extends { _tag: string } ? E["_tag"] : never, E, A1, E1, R1>(
k: K,
f: (e: NoInfer<Extract<E, { _tag: K }>>) => Effect<A1, E1, R1>
): <A, R>(self: Effect<A, E, R>) => Effect<A1 | A, E1 | Exclude<E, { _tag: K }>, R1 | R>
<A, E, R, K extends E extends { _tag: string } ? E["_tag"] : never, R1, E1, A1>(
self: Effect<A, E, R>,
k: K,
f: (e: Extract<E, { _tag: K }>) => Effect<A1, E1, R1>
): Effect<A | A1, E1 | Exclude<E, { _tag: K }>, R | R1>
} = core.catchTag
/**
* Handles both recoverable and unrecoverable errors by providing a recovery
* effect.
*
* **When to Use**
*
* The `catchAllCause` function allows you to handle all errors, including
* unrecoverable defects, by providing a recovery effect. The recovery logic is
* based on the `Cause` of the error, which provides detailed information about
* the failure.
*
* **When to Recover from Defects**
*
* Defects are unexpected errors that typically shouldn't be recovered from, as
* they often indicate serious issues. However, in some cases, such as
* dynamically loaded plugins, controlled recovery might be needed.
*
* @example
* ```ts
* // Title: Recovering from All Errors
* import { Cause, Effect } from "effect"
*
* // Define an effect that may fail with a recoverable or unrecoverable error
* const program = Effect.fail("Something went wrong!")
*
* // Recover from all errors by examining the cause
* const recovered = program.pipe(
* Effect.catchAllCause((cause) =>
* Cause.isFailType(cause)
* ? Effect.succeed("Recovered from a regular error")
* : Effect.succeed("Recovered from a defect")
* )
* )
*
* Effect.runPromise(recovered).then(console.log)
* // Output: "Recovered from a regular error"
* ```
*
* @since 4.0.0
* @category Error handling
*/
export const catchCause: {
<E, A2, E2, R2>(
f: (cause: Cause<E>) => Effect<A2, E2, R2>
): <A, R>(self: Effect<A, E, R>) => Effect<A2 | A, E2, R2 | R>
<A, E, R, A2, E2, R2>(
self: Effect<A, E, R>,
f: (cause: Cause<E>) => Effect<A2, E2, R2>
): Effect<A | A2, E2, R | R2>
} = core.catchCause
/**
* Recovers from all defects using a provided recovery function.
*
* **When to Use**
*
* There is no sensible way to recover from defects. This method should be used
* only at the boundary between Effect and an external system, to transmit
* information on a defect for diagnostic or explanatory purposes.
*
* **Details**
*
* `catchAllDefect` allows you to handle defects, which are unexpected errors
* that usually cause the program to terminate. This function lets you recover
* from these defects by providing a function that handles the error. However,
* it does not handle expected errors (like those from {@link fail}) or
* execution interruptions (like those from {@link interrupt}).
*
* **When to Recover from Defects**
*
* Defects are unexpected errors that typically shouldn't be recovered from, as
* they often indicate serious issues. However, in some cases, such as
* dynamically loaded plugins, controlled recovery might be needed.
*
* @example
* ```ts
* // Title: Handling All Defects
* import { Effect, Cause, Console } from "effect"
*
* // Simulating a runtime error
* const task = Effect.dieMessage("Boom!")
*
* const program = Effect.catchAllDefect(task, (defect) => {
* if (Cause.isRuntimeException(defect)) {
* return Console.log(
* `RuntimeException defect caught: ${defect.message}`
* )
* }
* return Console.log("Unknown defect caught.")
* })
*
* // We get an Exit.Success because we caught all defects
* Effect.runPromiseExit(program).then(console.log)
* // Output:
* // RuntimeException defect caught: Boom!
* // {
* // _id: "Exit",
* // _tag: "Success",
* // value: undefined
* // }
* ```
*
* @since 4.0.0
* @category Error handling
*/
export const catchDefect: {
<A2, E2, R2>(
f: (defect: unknown) => Effect<A2, E2, R2>
): <A, E, R>(self: Effect<A, E, R>) => Effect<A2 | A, E2 | E, R2 | R>
<A, E, R, A2, E2, R2>(
self: Effect<A, E, R>,
f: (defect: unknown) => Effect<A2, E2, R2>
): Effect<A | A2, E | E2, R | R2>
} = core.catchDefect
/**
* Recovers from specific errors based on a predicate.
*
* **When to Use**
*
* `catchIf` works similarly to {@link catchSome}, but it allows you to
* recover from errors by providing a predicate function. If the predicate
* matches the error, the recovery effect is applied. This function doesn't
* alter the error type, so the resulting effect still carries the original
* error type unless a user-defined type guard is used to narrow the type.
*
* @example
* ```ts
* // Title: Catching Specific Errors with a Predicate
* import { Effect, Random } from "effect"
*
* class HttpError {
* readonly _tag = "HttpError"
* }
*
* class ValidationError {
* readonly _tag = "ValidationError"
* }
*
* // ┌─── Effect<string, HttpError | ValidationError, never>
* // ▼
* const program = Effect.gen(function* () {
* const n1 = yield* Random.next
* const n2 = yield* Random.next
* if (n1 < 0.5) {
* yield* Effect.fail(new HttpError())
* }
* if (n2 < 0.5) {
* yield* Effect.fail(new ValidationError())
* }
* return "some result"
* })
*
* // ┌─── Effect<string, ValidationError, never>
* // ▼
* const recovered = program.pipe(
* Effect.catchIf(
* // Only handle HttpError errors
* (error) => error._tag === "HttpError",
* () => Effect.succeed("Recovering from HttpError")
* )
* )
* ```
*
* @since 2.0.0
* @category Error handling
*/
export const catchIf: {
<E, EB extends E, A2, E2, R2>(
refinement: Refinement<NoInfer<E>, EB>,
f: (e: EB) => Effect<A2, E2, R2>
): <A, R>(
self: Effect<A, E, R>
) => Effect<A2 | A, E2 | Exclude<E, EB>, R2 | R>
<E, A2, E2, R2>(
predicate: Predicate<NoInfer<E>>,
f: (e: NoInfer<E>) => Effect<A2, E2, R2>
): <A, R>(self: Effect<A, E, R>) => Effect<A2 | A, E | E2, R2 | R>
<A, E, R, EB extends E, A2, E2, R2>(
self: Effect<A, E, R>,
refinement: Refinement<E, EB>,
f: (e: EB) => Effect<A2, E2, R2>
): Effect<A | A2, E2 | Exclude<E, EB>, R | R2>
<A, E, R, A2, E2, R2>(
self: Effect<A, E, R>,
predicate: Predicate<E>,
f: (e: E) => Effect<A2, E2, R2>
): Effect<A | A2, E | E2, R | R2>
} = core.catchIf
/**
* Recovers from specific failures based on a predicate.
*
* @since 4.0.0
* @category Error handling
*/
export const catchFailure: {
<E, B, E2, R2, EB extends Failure<E>>(
refinement: Refinement<Failure<E>, EB>,
f: (failure: EB, cause: Cause<E>) => Effect<B, E2, R2>
): <A, R>(
self: Effect<A, E, R>
) => Effect<A | B, Exclude<E, Failure.Error<EB>> | E2, R | R2>
<E, B, E2, R2>(
predicate: Predicate<Failure<NoInfer<E>>>,
f: (failure: NoInfer<Failure<E>>, cause: Cause<E>) => Effect<B, E2, R2>
): <A, R>(self: Effect<A, E, R>) => Effect<A | B, E | E2, R | R2>
<A, E, R, B, E2, R2, EB extends Failure<E>>(
self: Effect<A, E, R>,
refinement: Refinement<Failure<E>, EB>,
f: (failure: EB, cause: Cause<E>) => Effect<B, E2, R2>
): Effect<A | B, Exclude<E, Failure.Error<EB>> | E2, R | R2>
<A, E, R, B, E2, R2>(
self: Effect<A, E, R>,
predicate: Predicate<Failure<NoInfer<E>>>,
f: (failure: NoInfer<Failure<E>>, cause: Cause<E>) => Effect<B, E2, R2>
): Effect<A | B, E | E2, R | R2>
} = core.catchFailure
/**
* The `mapError` function is used to transform or modify the error
* produced by an effect, without affecting its success value.
*
* This function is helpful when you want to enhance the error with additional
* information, change the error type, or apply custom error handling while
* keeping the original behavior of the effect's success values intact. It only
* operates on the error channel and leaves the success channel unchanged.
*
* @see {@link map} for a version that operates on the success channel.
* @see {@link mapBoth} for a version that operates on both channels.
* @see {@link orElseFail} if you want to replace the error with a new one.
*
* @example
* ```ts
* import { Effect } from "effect"
*
* // ┌─── Effect<number, string, never>
* // ▼
* const simulatedTask = Effect.fail("Oh no!").pipe(Effect.as(1))
*
* // ┌─── Effect<number, Error, never>
* // ▼
* const mapped = Effect.mapError(
* simulatedTask,
* (message) => new Error(message)
* )
* ```
*
* @since 2.0.0
* @category Error handling
*/
export const mapError: {
<E, E2>(f: (e: E) => E2): <A, R>(self: Effect<A, E, R>) => Effect<A, E2, R>
<A, E, R, E2>(self: Effect<A, E, R>, f: (e: E) => E2): Effect<A, E2, R>
} = core.mapError
/**
* Converts an effect's failure into a fiber termination, removing the error from the effect's type.
*
* **When to Use*
*
* Use `orDie` when failures should be treated as unrecoverable defects and no error handling is required.
*
* **Details**
*
* The `orDie` function is used when you encounter errors that you do not want to handle or recover from.
* It removes the error type from the effect and ensures that any failure will terminate the fiber.
* This is useful for propagating failures as defects, signaling that they should not be handled within the effect.
*
* @see {@link orDieWith} if you need to customize the error.
*
* @example
* ```ts
* // Title: Propagating an Error as a Defect
* import { Effect } from "effect"
*
* const divide = (a: number, b: number) =>
* b === 0
* ? Effect.fail(new Error("Cannot divide by zero"))
* : Effect.succeed(a / b)
*
* // ┌─── Effect<number, never, never>
* // ▼
* const program = Effect.orDie(divide(1, 0))
*
* Effect.runPromise(program).catch(console.error)
* // Output:
* // (FiberFailure) Error: Cannot divide by zero
* // ...stack trace...
* ```
*
* @since 2.0.0
* @category Converting Failures to Defects
*/
export const orDie: <A, E, R>(self: Effect<A, E, R>) => Effect<A, never, R> = core.orDie
/**
* The `tapError` function executes an effectful operation to inspect the
* failure of an effect without modifying it.
*
* This function is useful when you want to perform some side effect (like
* logging or tracking) on the failure of an effect, but without changing the
* result of the effect itself. The error remains in the effect's error channel,
* while the operation you provide can inspect or act on it.
*
* @example
* ```ts
* import { Effect, Console } from "effect"
*
* // Simulate a task that fails with an error
* const task: Effect.Effect<number, string> = Effect.fail("NetworkError")
*
* // Use tapError to log the error message when the task fails
* const tapping = Effect.tapError(task, (error) =>
* Console.log(`expected error: ${error}`)
* )
*
* Effect.runFork(tapping)
* // Output:
* // expected error: NetworkError
* ```
*
* @since 2.0.0
* @category sequencing
*/
export const tapError: {
<E, X, E2, R2>(
f: (e: NoInfer<E>) => Effect<X, E2, R2>
): <A, R>(self: Effect<A, E, R>) => Effect<A, E | E2, R2 | R>
<A, E, R, X, E2, R2>(self: Effect<A, E, R>, f: (e: E) => Effect<X, E2, R2>): Effect<A, E | E2, R | R2>
} = core.tapError
/**
* The `tapErrorCause` function allows you to inspect the complete cause
* of an error, including failures and defects.
*
* This function is helpful when you need to log, monitor, or handle specific
* error causes in your effects. It gives you access to the full error cause,
* whether it’s a failure, defect, or other exceptional conditions, without
* altering the error or the overall result of the effect.
*
* @example
* ```ts
* import { Effect, Console } from "effect"
*
* // Create a task that fails with a NetworkError
* const task1: Effect.Effect<number, string> = Effect.fail("NetworkError")
*
* const tapping1 = Effect.tapErrorCause(task1, (cause) =>
* Console.log(`error cause: ${cause}`)
* )
*
* Effect.runFork(tapping1)
* // Output:
* // error cause: Error: NetworkError
*
* // Simulate a severe failure in the system
* const task2: Effect.Effect<number, string> = Effect.dieMessage(
* "Something went wrong"
* )
*
* const tapping2 = Effect.tapErrorCause(task2, (cause) =>
* Console.log(`error cause: ${cause}`)
* )
*
* Effect.runFork(tapping2)
* // Output:
* // error cause: RuntimeException: Something went wrong
* // ... stack trace ...
* ```
*
* @since 2.0.0
* @category sequencing
*/
export const tapCause: {
<E, X, E2, R2>(
f: (cause: Cause<NoInfer<E>>) => Effect<X, E2, R2>
): <A, R>(self: Effect<A, E, R>) => Effect<A, E | E2, R2 | R>
<A, E, R, X, E2, R2>(
self: Effect<A, E, R>,
f: (cause: Cause<E>) => Effect<X, E2, R2>
): Effect<A, E | E2, R | R2>
} = core.tapCause
// -----------------------------------------------------------------------------
// Error Handling
// -----------------------------------------------------------------------------
/**
* @since 2.0.0
* @category Error handling
*/
export declare namespace Retry {
/**
* @since 2.0.0
* @category Error handling
*/
export type Return<R, E, A, O extends Options<E>> = Effect<
A,
| (O extends { schedule: Schedule<infer _O, infer _I, infer _R> } ? E
: O extends { until: Refinement<E, infer E2> } ? E2
: E)
| (O extends { while: (...args: Array<any>) => Effect<infer _A, infer E, infer _R> } ? E : never)
| (O extends { until: (...args: Array<any>) => Effect<infer _A, infer E, infer _R> } ? E : never),
| R
| (O extends { schedule: Schedule<infer _O, infer _I, infer R> } ? R : never)
| (O extends { while: (...args: Array<any>) => Effect<infer _A, infer _E, infer R> } ? R : never)
| (O extends { until: (...args: Array<any>) => Effect<infer _A, infer _E, infer R> } ? R : never)
> extends infer Z ? Z : never
/**
* @since 2.0.0
* @category Error handling
*/
export interface Options<E> {
while?: ((error: E) => boolean | Effect<boolean, any, any>) | undefined
until?: ((error: E) => boolean | Effect<boolean, any, any>) | undefined
times?: number | undefined
schedule?: Schedule<any, E, any> | undefined
}
}
/**
* Retries a failing effect based on a defined retry policy.
*
* **Details**
*
* The `Effect.retry` function takes an effect and a {@link Schedule} policy,
* and will automatically retry the effect if it fails, following the rules of
* the policy.
*
* If the effect ultimately succeeds, the result will be returned.
*
* If the maximum retries are exhausted and the effect still fails, the failure
* is propagated.
*
* **When to Use**
*
* This can be useful when dealing with intermittent failures, such as network
* issues or temporary resource unavailability. By defining a retry policy, you
* can control the number of retries, the delay between them, and when to stop
* retrying.
*
* @see {@link retryOrElse} for a version that allows you to run a fallback.
* @see {@link repeat} if your retry condition is based on successful outcomes rather than errors.
*
* @example
* ```ts
* // Title: Retrying with a Fixed Delay
* import { Effect, Schedule } from "effect"
*
* let count = 0
*
* // Simulates an effect with possible failures
* const task = Effect.async<string, Error>((resume) => {
* if (count <= 2) {
* count++
* console.log("failure")
* resume(Effect.fail(new Error()))
* } else {
* console.log("success")
* resume(Effect.succeed("yay!"))
* }
* })
*
* // Define a repetition policy using a fixed delay between retries
* const policy = Schedule.fixed("100 millis")
*
* const repeated = Effect.retry(task, policy)
*
* Effect.runPromise(repeated).then(console.log)
* // Output:
* // failure
* // failure
* // failure
* // success
* // yay!
* ```
*
* @example
* ```ts
* // Title: Retrying a Task up to 5 times
* import { Effect } from "effect"
*
* let count = 0
*
* // Simulates an effect with possible failures
* const task = Effect.async<string, Error>((resume) => {
* if (count <= 2) {
* count++
* console.log("failure")
* resume(Effect.fail(new Error()))
* } else {
* console.log("success")
* resume(Effect.succeed("yay!"))
* }
* })
*
* // Retry the task up to 5 times
* Effect.runPromise(Effect.retry(task, { times: 5 }))
* // Output:
* // failure
* // failure
* // failure
* // success
* ```
*
* @example
* ```ts
* // Title: Retrying Until a Specific Condition is Met
* import { Effect } from "effect"
*
* let count = 0
*
* // Define an effect that simulates varying error on each invocation
* const action = Effect.failSync(() => {
* console.log(`Action called ${++count} time(s)`)
* return `Error ${count}`
* })
*
* // Retry the action until a specific condition is met
* const program = Effect.retry(action, {
* until: (err) => err === "Error 3"
* })
*
* Effect.runPromiseExit(program).then(console.log)
* // Output:
* // Action called 1 time(s)
* // Action called 2 time(s)
* // Action called 3 time(s)
* // {
* // _id: 'Exit',
* // _tag: 'Failure',
* // cause: { _id: 'Cause', _tag: 'Fail', failure: 'Error 3' }
* // }
* ```
*
* @since 2.0.0
* @category Error handling
*/
export const retry: {
<E, O extends Retry.Options<E>>(options: O): <A, R>(self: Effect<A, E, R>) => Retry.Return<R, E, A, O>
<B, E, Error, Env>(
policy: Schedule<B, NoInfer<E>, Error, Env>
): <A, R>(self: Effect<A, E, R>) => Effect<A, E | Error, R | Env>
<A, E, R, O extends Retry.Options<E>>(self: Effect<A, E, R>, options: O): Retry.Return<R, E, A, O>
<A, E, R, B, Error, Env>(self: Effect<A, E, R>, policy: Schedule<B, E, Error, Env>): Effect<A, E | Error, R | Env>
} = internalSchedule.retry
/**
* Retries a failing effect and runs a fallback effect if retries are exhausted.
*
* **Details**
*
* The `Effect.retryOrElse` function attempts to retry a failing effect multiple
* times according to a defined {@link Schedule} policy.
*
* If the retries are exhausted and the effect still fails, it runs a fallback
* effect instead.
*
* **When to Use**
*
* This function is useful when you want to handle failures gracefully by
* specifying an alternative action after repeated failures.
*
* @see {@link retry} for a version that does not run a fallback effect.
*
* @example
* ```ts
* // Title: Retrying with Fallback
* import { Effect, Schedule, Console } from "effect"
*
* let count = 0
*
* // Simulates an effect with possible failures
* const task = Effect.async<string, Error>((resume) => {
* if (count <= 2) {
* count++
* console.log("failure")
* resume(Effect.fail(new Error()))
* } else {
* console.log("success")
* resume(Effect.succeed("yay!"))
* }
* })
*
* // Retry the task with a delay between retries and a maximum of 2 retries
* const policy = Schedule.addDelay(Schedule.recurs(2), () => "100 millis")
*
* // If all retries fail, run the fallback effect
* const repeated = Effect.retryOrElse(
* task,
* policy,
* // fallback
* () => Console.log("orElse").pipe(Effect.as("default value"))
* )
*
* // Effect.runPromise(repeated).then(console.log)
* // Output:
* // failure
* // failure
* // failure
* // orElse
* // default value
* ```
*
* @since 2.0.0
* @category Error handling
*/
export const retryOrElse: {
<A1, E, E1, R1, A2, E2, R2>(
policy: Schedule<A1, NoInfer<E>, E1, R1>,
orElse: (e: NoInfer<E | E1>, out: A1) => Effect<A2, E2, R2>
): <A, R>(self: Effect<A, E, R>) => Effect<A | A2, E1 | E2, R | R1 | R2>
<A, E, R, A1, E1, R1, A2, E2, R2>(
self: Effect<A, E, R>,
policy: Schedule<A1, NoInfer<E>, E1, R1>,
orElse: (e: NoInfer<E | E1>, out: A1) => Effect<A2, E2, R2>
): Effect<A | A2, E1 | E2, R | R1 | R2>
} = internalSchedule.retryOrElse
/**
* The `sandbox` function transforms an effect by exposing the full cause
* of any error, defect, or fiber interruption that might occur during its
* execution. It changes the error channel of the effect to include detailed
* information about the cause, which is wrapped in a `Cause<E>` type.
*
* This function is useful when you need access to the complete underlying cause
* of failures, defects, or interruptions, enabling more detailed error
* handling. Once you apply `sandbox`, you can use operators like
* {@link catchAll} and {@link catchTags} to handle specific error conditions.
* If necessary, you can revert the sandboxing operation with {@link unsandbox}
* to return to the original error handling behavior.
*
* @see {@link unsandbox} to restore the original error handling.
*
* @example
* ```ts
* import { Effect, Console } from "effect"
*
* // ┌─── Effect<string, Error, never>
* // ▼
* const task = Effect.fail(new Error("Oh uh!")).pipe(
* Effect.as("primary result")
* )
*
* // ┌─── Effect<string, Cause<Error>, never>
* // ▼
* const sandboxed = Effect.sandbox(task)
*
* const program = Effect.catchTags(sandboxed, {
* Die: (cause) =>
* Console.log(`Caught a defect: ${cause.defect}`).pipe(
* Effect.as("fallback result on defect")
* ),
* Interrupt: (cause) =>
* Console.log(`Caught a defect: ${cause.fiberId}`).pipe(
* Effect.as("fallback result on fiber interruption")
* ),
* Fail: (cause) =>
* Console.log(`Caught a defect: ${cause.error}`).pipe(
* Effect.as("fallback result on failure")
* )
* })
*
* // Restore the original error handling with unsandbox
* const main = Effect.unsandbox(program)
*
* Effect.runPromise(main).then(console.log)
* // Output:
* // Caught a defect: Oh uh!
* // fallback result on failure
* ```
*
* @since 2.0.0
* @category Error handling
*/
export const sandbox: <A, E, R>(self: Effect<A, E, R>) => Effect<A, Cause<E>, R> = core.sandbox
/**
* Discards both the success and failure values of an effect.
*
* **When to Use**
*
* `ignore` allows you to run an effect without caring about its result, whether
* it succeeds or fails. This is useful when you only care about the side
* effects of the effect and do not need to handle or process its outcome.
*
* @example
* ```ts
* // Title: Using Effect.ignore to Discard Values
* import { Effect } from "effect"
*
* // ┌─── Effect<number, string, never>
* // ▼
* const task = Effect.fail("Uh oh!").pipe(Effect.as(5))
*
* // ┌─── Effect<void, never, never>
* // ▼
* const program = Effect.ignore(task)
* ```
*
* @since 2.0.0
* @category Error handling
*/
export const ignore: <A, E, R>(self: Effect<A, E, R>) => Effect<void, never, R> = core.ignore
// -----------------------------------------------------------------------------
// Fallback
// -----------------------------------------------------------------------------
/**
* Replaces the original failure with a success value, ensuring the effect
* cannot fail.
*
* `orElseSucceed` allows you to replace the failure of an effect with a
* success value. If the effect fails, it will instead succeed with the provided
* value, ensuring the effect always completes successfully. This is useful when
* you want to guarantee a successful result regardless of whether the original
* effect failed.
*
* The function ensures that any failure is effectively "swallowed" and replaced
* by a successful value, which can be helpful for providing default values in
* case of failure.
*
* **Important**: This function only applies to failed effects. If the effect
* already succeeds, it will remain unchanged.
*
* @example
* ```ts
* 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 }
* ```
*
* @since 2.0.0
* @category Fallback
*/
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>
} = core.orElseSucceed
// -----------------------------------------------------------------------------
// Delays & timeouts
// -----------------------------------------------------------------------------
/**
* Adds a time limit to an effect, triggering a timeout if the effect exceeds
* the duration.
*
* The `timeout` function allows you to specify a time limit for an
* effect's execution. If the effect does not complete within the given time, a
* `TimeoutException` is raised. This can be useful for controlling how long
* your program waits for a task to finish, ensuring that it doesn't hang
* indefinitely if the task takes too long.
*
* @see {@link timeoutFail} for a version that raises a custom error.
* @see {@link timeoutFailCause} for a version that raises a custom defect.
* @see {@link timeoutTo} for a version that allows specifying both success and timeout handlers.
*
* @example
* ```ts
* import { Effect } from "effect"
*
* const task = Effect.gen(function* () {
* console.log("Start processing...")
* yield* Effect.sleep("2 seconds") // Simulates a delay in processing
* console.log("Processing complete.")
* return "Result"
* })
*
* // Output will show a TimeoutException as the task takes longer
* // than the specified timeout duration
* const timedEffect = task.pipe(Effect.timeout("1 second"))
*
* Effect.runPromiseExit(timedEffect).then(console.log)
* // Output:
* // Start processing...
* // {
* // _id: 'Exit',
* // _tag: 'Failure',
* // cause: {
* // _id: 'Cause',
* // _tag: 'Fail',
* // failure: { _tag: 'TimeoutException' }
* // }
* // }
* ```
*
* @since 2.0.0
* @category delays & timeouts
*/
export const timeout: {
(duration: DurationInput): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E | TimeoutError, R>
<A, E, R>(self: Effect<A, E, R>, duration: DurationInput): Effect<A, E | TimeoutError, R>
} = core.timeout
/**
* Handles timeouts by returning an `Option` that represents either the result
* or a timeout.
*
* The `timeoutOption` function provides a way to gracefully handle
* timeouts by wrapping the outcome of an effect in an `Option` type. If the
* effect completes within the specified time, it returns a `Some` containing
* the result. If the effect times out, it returns a `None`, allowing you to
* treat the timeout as a regular result instead of throwing an error.
*
* This is useful when you want to handle timeouts without causing the program
* to fail, making it easier to manage situations where you expect tasks might
* take too long but want to continue executing other tasks.
*
* @see {@link timeout} for a version that raises a `TimeoutException`.
* @see {@link timeoutFail} for a version that raises a custom error.
* @see {@link timeoutFailCause} for a version that raises a custom defect.
* @see {@link timeoutTo} for a version that allows specifying both success and timeout handlers.
*
* @example
* ```ts
* import { Effect } from "effect"
*
* const task = Effect.gen(function* () {
* console.log("Start processing...")
* yield* Effect.sleep("2 seconds") // Simulates a delay in processing
* console.log("Processing complete.")
* return "Result"
* })
*
* const timedOutEffect = Effect.all([
* task.pipe(Effect.timeoutOption("3 seconds")),
* task.pipe(Effect.timeoutOption("1 second"))
* ])
*
* Effect.runPromise(timedOutEffect).then(console.log)
* // Output:
* // Start processing...
* // Processing complete.
* // Start processing...
* // [
* // { _id: 'Option', _tag: 'Some', value: 'Result' },
* // { _id: 'Option', _tag: 'None' }
* // ]
* ```
*
* @since 3.1.0
* @category delays & timeouts
*/
export const timeoutOption: {
(duration: DurationInput): <A, E, R>(self: Effect<A, E, R>) => Effect<Option.Option<A>, E, R>
<A, E, R>(self: Effect<A, E, R>, duration: DurationInput): Effect<Option.Option<A>, E, R>
} = core.timeoutOption
/**
* @since 3.1.0
* @category delays & timeouts
*/
export const timeoutOrElse: {
<A2, E2, R2>(
options: { readonly duration: DurationInput; readonly onTimeout: LazyArg<Effect<A2, E2, R2>> }
): <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>,
options: { readonly duration: DurationInput; readonly onTimeout: LazyArg<Effect<A2, E2, R2>> }
): Effect<A | A2, E | E2, R | R2>
} = core.timeoutOrElse
/**
* Returns an effect that is delayed from this effect by the specified
* `Duration`.
*
* @since 2.0.0
* @category delays & timeouts
*/
export const delay: {
(duration: DurationInput): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, R>
<A, E, R>(self: Effect<A, E, R>, duration: DurationInput): Effect<A, E, R>
} = core.delay
/**
* Returns an effect that suspends for the specified duration. This method is
* asynchronous, and does not actually block the fiber executing the effect.
*
* @since 2.0.0
* @category delays & timeouts
*/
export const sleep: (duration: DurationInput) => Effect<void> = core.sleep
// -----------------------------------------------------------------------------
// Racing
// -----------------------------------------------------------------------------
/**
* Races multiple effects and returns the first successful result.
*
* **Details**
*
* This function runs multiple effects concurrently and returns the result of
* the first one to succeed. If one effect succeeds, the others will be
* interrupted.
*
* If none of the effects succeed, the function will fail with the last error
* encountered.
*
* **When to Use**
*
* This is useful when you want to race multiple effects, but only care about
* the first one to succeed. It is commonly used in cases like timeouts,
* retries, or when you want to optimize for the faster response without
* worrying about the other effects.
*
* @see {@link race} for a version that handles only two effects.
*
* @example
* ```ts
* // Title: All Tasks Succeed
* import { Effect, Console } from "effect"
*
* const task1 = Effect.succeed("task1").pipe(
* Effect.delay("100 millis"),
* Effect.tap(Console.log("task1 done")),
* Effect.onInterrupt(() => Console.log("task1 interrupted"))
* )
* const task2 = Effect.succeed("task2").pipe(
* Effect.delay("200 millis"),
* Effect.tap(Console.log("task2 done")),
* Effect.onInterrupt(() => Console.log("task2 interrupted"))
* )
*
* const task3 = Effect.succeed("task3").pipe(
* Effect.delay("150 millis"),
* Effect.tap(Console.log("task3 done")),
* Effect.onInterrupt(() => Console.log("task3 interrupted"))
* )
*
* const program = Effect.raceAll([task1, task2, task3])
*
* Effect.runFork(program)
* // Output:
* // task1 done
* // task2 interrupted
* // task3 interrupted
* ```
*
* @example
* ```ts
* // Title: One Task Fails, Two Tasks Succeed
* import { Effect, Console } from "effect"
*
* const task1 = Effect.fail("task1").pipe(
* Effect.delay("100 millis"),
* Effect.tap(Console.log("task1 done")),
* Effect.onInterrupt(() => Console.log("task1 interrupted"))
* )
* const task2 = Effect.succeed("task2").pipe(
* Effect.delay("200 millis"),
* Effect.tap(Console.log("task2 done")),
* Effect.onInterrupt(() => Console.log("task2 interrupted"))
* )
*
* const task3 = Effect.succeed("task3").pipe(
* Effect.delay("150 millis"),
* Effect.tap(Console.log("task3 done")),
* Effect.onInterrupt(() => Console.log("task3 interrupted"))
* )
*
* const program = Effect.raceAll([task1, task2, task3])
*
* Effect.runFork(program)
* // Output:
* // task3 done
* // task2 interrupted
* ```
*
* @example
* ```ts
* // Title: All Tasks Fail
* import { Effect, Console } from "effect"
*
* const task1 = Effect.fail("task1").pipe(
* Effect.delay("100 millis"),
* Effect.tap(Console.log("task1 done")),
* Effect.onInterrupt(() => Console.log("task1 interrupted"))
* )
* const task2 = Effect.fail("task2").pipe(
* Effect.delay("200 millis"),
* Effect.tap(Console.log("task2 done")),
* Effect.onInterrupt(() => Console.log("task2 interrupted"))
* )
*
* const task3 = Effect.fail("task3").pipe(
* Effect.delay("150 millis"),
* Effect.tap(Console.log("task3 done")),
* Effect.onInterrupt(() => Console.log("task3 interrupted"))
* )
*
* const program = Effect.raceAll([task1, task2, task3])
*
* Effect.runPromiseExit(program).then(console.log)
* // Output:
* // {
* // _id: 'Exit',
* // _tag: 'Failure',
* // cause: { _id: 'Cause', _tag: 'Fail', failure: 'task2' }
* // }
* ```
*
* @since 2.0.0
* @category Racing
*/
export const raceAll: <Eff extends Effect<any, any, any>>(
all: Iterable<Eff>,
options?: {
readonly onWinner?: (
options: { readonly fiber: Fiber<any, any>; readonly index: number; readonly parentFiber: Fiber<any, any> }
) => void
}
) => Effect<Effect.Success<Eff>, Effect.Error<Eff>, Effect.Context<Eff>> = core.raceAll
/**
* @since 4.0.0
* @category Racing
*/
export const raceAllFirst: <Eff extends Effect<any, any, any>>(
all: Iterable<Eff>,
options?: {
readonly onWinner?: (
options: { readonly fiber: Fiber<any, any>; readonly index: number; readonly parentFiber: Fiber<any, any> }
) => void
}
) => Effect<Effect.Success<Eff>, Effect.Error<Eff>, Effect.Context<Eff>> = core.raceAllFirst
// -----------------------------------------------------------------------------
// Filtering
// -----------------------------------------------------------------------------
/**
* Filters an iterable using the specified effectful predicate.
*
* **Details**
*
* This function filters a collection (an iterable) by applying an effectful
* predicate.
*
* The predicate is a function that takes an element and its index, and it
* returns an effect that evaluates to a boolean.
*
* The function processes each element in the collection and keeps only those
* that satisfy the condition defined by the predicate.
*
* **Options**
*
* You can also adjust the behavior with options such as concurrency, batching,
* or whether to negate the condition.
*
* **When to Use**
*
* This function allows you to selectively keep or remove elements based on a
* condition that may involve asynchronous or side-effect-causing operations.
*
* @example
* ```ts
* import { Effect } from "effect"
*
* const numbers = [1, 2, 3, 4, 5]
* const predicate = (n: number, i: number) => Effect.succeed(n % 2 === 0)
*
* const program = Effect.gen(function*() {
* const result = yield* Effect.filter(numbers, predicate)
* console.log(result)
* })
*
* Effect.runFork(program)
* // Output: [2, 4]
* ```
*
* @since 2.0.0
* @category Filtering
*/
export const filter: <A, E, R>(
iterable: Iterable<A>,
f: (a: NoInfer<A>) => Effect<boolean, E, R>,
options?: { readonly concurrency?: Concurrency | undefined; readonly negate?: boolean | undefined }
) => Effect<Array<A>, E, R> = core.filter
// -----------------------------------------------------------------------------
// Pattern matching
// -----------------------------------------------------------------------------
/**
* Handles both success and failure cases of an effect without performing side
* effects.
*
* **Details**
*
* `match` lets you define custom handlers for both success and failure
* scenarios. You provide separate functions to handle each case, allowing you
* to process the result if the effect succeeds, or handle the error if the
* effect fails.
*
* **When to Use**
*
* This is useful for structuring your code to respond differently to success or
* failure without triggering side effects.
*
* @see {@link matchEffect} if you need to perform side effects in the handlers.
*
* @example
* ```ts
* // Title: Handling Both Success and Failure Cases
* import { Effect } from "effect"
*
* const success: Effect.Effect<number, Error> = Effect.succeed(42)
*
* const program1 = Effect.match(success, {
* onFailure: (error) => `failure: ${error.message}`,
* onSuccess: (value) => `success: ${value}`
* })
*
* // Run and log the result of the successful effect
* Effect.runPromise(program1).then(console.log)
* // Output: "success: 42"
*
* const failure: Effect.Effect<number, Error> = Effect.fail(
* new Error("Uh oh!")
* )
*
* const program2 = Effect.match(failure, {
* onFailure: (error) => `failure: ${error.message}`,
* onSuccess: (value) => `success: ${value}`
* })
*
* // Run and log the result of the failed effect
* Effect.runPromise(program2).then(console.log)
* // Output: "failure: Uh oh!"
* ```
*
* @since 2.0.0
* @category Pattern matching
*/
export const match: {
<E, A2, A, A3>(options: {
readonly onFailure: (error: E) => A2
readonly onSuccess: (value: A) => A3
}): <R>(self: Effect<A, E, R>) => Effect<A2 | A3, never, R>
<A, E, R, A2, A3>(
self: Effect<A, E, R>,
options: {
readonly onFailure: (error: E) => A2
readonly onSuccess: (value: A) => A3
}
): Effect<A2 | A3, never, R>
} = core.match
/**
* Handles failures by matching the cause of failure.
*
* **Details**
*
* The `matchCause` function allows you to handle failures with access to the
* full cause of the failure within a fiber.
*
* **When to Use**
*
* This is useful for differentiating between different types of errors, such as
* regular failures, defects, or interruptions. You can provide specific
* handling logic for each failure type based on the cause.
*
* @see {@link matchCauseEffect} if you need to perform side effects in the
* handlers.
* @see {@link match} if you don't need to handle the cause of the failure.
*
* @example
* ```ts
* // Title: Handling Different Failure Causes
* import { Effect } from "effect"
*
* const task: Effect.Effect<number, Error> = Effect.die("Uh oh!")
*
* const program = Effect.matchCause(task, {
* onFailure: (cause) => {
* switch (cause._tag) {
* case "Fail":
* // Handle standard failure
* return `Fail: ${cause.error.message}`
* case "Die":
* // Handle defects (unexpected errors)
* return `Die: ${cause.defect}`
* case "Interrupt":
* // Handle interruption
* return `${cause.fiberId} interrupted!`
* }
* // Fallback for other causes
* return "failed due to other causes"
* },
* onSuccess: (value) =>
* // task completes successfully
* `succeeded with ${value} value`
* })
*
* Effect.runPromise(program).then(console.log)
* // Output: "Die: Uh oh!"
*
* ```
*
* @since 2.0.0
* @category Pattern matching
*/
export const matchCause: {
<E, A2, A, A3>(options: {
readonly onFailure: (cause: Cause<E>) => A2
readonly onSuccess: (a: A) => A3
}): <R>(self: Effect<A, E, R>) => Effect<A2 | A3, never, R>
<A, E, R, A2, A3>(
self: Effect<A, E, R>,
options: {
readonly onFailure: (cause: Cause<E>) => A2
readonly onSuccess: (a: A) => A3
}
): Effect<A2 | A3, never, R>
} = core.matchCause
/**
* Handles failures with access to the cause and allows performing side effects.
*
* **Details**
*
* The `matchCauseEffect` function works similarly to {@link matchCause}, but it
* also allows you to perform additional side effects based on the failure
* cause. This function provides access to the complete cause of the failure,
* making it possible to differentiate between various failure types, and allows
* you to respond accordingly while performing side effects (like logging or
* other operations).
*
* @see {@link matchCause} if you don't need side effects and only want to handle the result or failure.
* @see {@link matchEffect} if you don't need to handle the cause of the failure.
*
* @example
* ```ts
* // Title: Handling Different Failure Causes with Side Effects
* import { Effect, Console } from "effect"
*
* const task: Effect.Effect<number, Error> = Effect.die("Uh oh!")
*
* const program = Effect.matchCauseEffect(task, {
* onFailure: (cause) => {
* switch (cause._tag) {
* case "Fail":
* // Handle standard failure with a logged message
* return Console.log(`Fail: ${cause.error.message}`)
* case "Die":
* // Handle defects (unexpected errors) by logging the defect
* return Console.log(`Die: ${cause.defect}`)
* case "Interrupt":
* // Handle interruption and log the fiberId that was interrupted
* return Console.log(`${cause.fiberId} interrupted!`)
* }
* // Fallback for other causes
* return Console.log("failed due to other causes")
* },
* onSuccess: (value) =>
* // Log success if the task completes successfully
* Console.log(`succeeded with ${value} value`)
* })
*
* Effect.runPromise(program)
* // Output: "Die: Uh oh!"
* ```
*
* @since 2.0.0
* @category Pattern matching
*/
export const matchCauseEffect: {
<E, A2, E2, R2, A, A3, E3, R3>(options: {
readonly onFailure: (cause: Cause<E>) => Effect<A2, E2, R2>
readonly onSuccess: (a: A) => Effect<A3, E3, R3>
}): <R>(self: Effect<A, E, R>) => Effect<A2 | A3, E2 | E3, R2 | R3 | R>
<A, E, R, A2, E2, R2, A3, E3, R3>(
self: Effect<A, E, R>,
options: {
readonly onFailure: (cause: Cause<E>) => Effect<A2, E2, R2>
readonly onSuccess: (a: A) => Effect<A3, E3, R3>
}
): Effect<A2 | A3, E2 | E3, R2 | R3 | R>
} = core.matchCauseEffect
/**
* Handles both success and failure cases of an effect, allowing for additional
* side effects.
*
* **Details**
*
* The `matchEffect` function is similar to {@link match}, but it enables you to
* perform side effects in the handlers for both success and failure outcomes.
*
* **When to Use**
*
* This is useful when you need to execute additional actions, like logging or
* notifying users, based on whether an effect succeeds or fails.
*
* @see {@link match} if you don't need side effects and only want to handle the
* result or failure.
*
* @example
* ```ts
* // Title: Handling Both Success and Failure Cases with Side Effects
* import { Effect } from "effect"
*
* const success: Effect.Effect<number, Error> = Effect.succeed(42)
* const failure: Effect.Effect<number, Error> = Effect.fail(
* new Error("Uh oh!")
* )
*
* const program1 = Effect.matchEffect(success, {
* onFailure: (error) =>
* Effect.succeed(`failure: ${error.message}`).pipe(
* Effect.tap(Effect.log)
* ),
* onSuccess: (value) =>
* Effect.succeed(`success: ${value}`).pipe(Effect.tap(Effect.log))
* })
*
* console.log(Effect.runSync(program1))
* // Output:
* // timestamp=... level=INFO fiber=#0 message="success: 42"
* // success: 42
*
* const program2 = Effect.matchEffect(failure, {
* onFailure: (error) =>
* Effect.succeed(`failure: ${error.message}`).pipe(
* Effect.tap(Effect.log)
* ),
* onSuccess: (value) =>
* Effect.succeed(`success: ${value}`).pipe(Effect.tap(Effect.log))
* })
*
* console.log(Effect.runSync(program2))
* // Output:
* // timestamp=... level=INFO fiber=#1 message="failure: Uh oh!"
* // failure: Uh oh!
* ```
*
* @since 2.0.0
* @category Pattern matching
*/
export const matchEffect: {
<E, A2, E2, R2, A, A3, E3, R3>(options: {
readonly onFailure: (e: E) => Effect<A2, E2, R2>
readonly onSuccess: (a: A) => Effect<A3, E3, R3>
}): <R>(self: Effect<A, E, R>) => Effect<A2 | A3, E2 | E3, R2 | R3 | R>
<A, E, R, A2, E2, R2, A3, E3, R3>(
self: Effect<A, E, R>,
options: {
readonly onFailure: (e: E) => Effect<A2, E2, R2>
readonly onSuccess: (a: A) => Effect<A3, E3, R3>
}
): Effect<A2 | A3, E2 | E3, R2 | R3 | R>
} = core.matchEffect
// -----------------------------------------------------------------------------
// Environment
// -----------------------------------------------------------------------------
/**
* @since 2.0.0
* @category Environment
*/
export const context: <R>() => Effect<Context<R>, never, R> = core.context
/**
* @since 2.0.0
* @category Environment
*/
export const provide: {
<const Layers extends [Layer.Any, ...Array<Layer.Any>]>(
layers: Layers
): <A, E, R>(
self: Effect<A, E, R>
) => Effect<
A,
E | Layer.Error<Layers[number]>,
Layer.Context<Layers[number]> | Exclude<R, Layer.Success<Layers[number]>>
>
<ROut, E2, RIn>(
layer: Layer<ROut, E2, RIn>
): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E | E2, RIn | Exclude<R, ROut>>
<R2>(context: Context<R2>): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, Exclude<R, R2>>
<A, E, R, const Layers extends [Layer.Any, ...Array<Layer.Any>]>(
self: Effect<A, E, R>,
layers: Layers
): Effect<
A,
E | Layer.Error<Layers[number]>,
Layer.Context<Layers[number]> | Exclude<R, Layer.Success<Layers[number]>>
>
<A, E, R, ROut, E2, RIn>(
self: Effect<A, E, R>,
layer: Layer<ROut, E2, RIn>
): Effect<A, E | E2, RIn | Exclude<R, ROut>>
<A, E, R, R2>(self: Effect<A, E, R>, context: Context<R2>): Effect<A, E, Exclude<R, R2>>
} = internalLayer.provide
/**
* @since 2.0.0
* @category Environment
*/
export const provideContext: {
<XR>(
context: Context<XR>
): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, Exclude<R, XR>>
<A, E, R, XR>(
self: Effect<A, E, R>,
context: Context<XR>
): Effect<A, E, Exclude<R, XR>>
} = core.provideContext
/**
* @since 4.0.0
* @category Environment
*/
export const service: {
<I, S>(tag: Reference<I, S>): Effect<S>
<I, S>(tag: Tag<I, S>): Effect<S, never, I>
} = core.service
/**
* @since 2.0.0
* @category Context
*/
export const serviceOption: <I, S>(tag: Tag<I, S>) => Effect<Option.Option<S>> = core.serviceOption
/**
* Updates the service with the required service entry.
*
* @since 2.0.0
* @category Context
*/
export const updateService: {
<I, A>(tag: Reference<I, A>, f: (value: A) => A): <XA, E, R>(self: Effect<XA, E, R>) => Effect<XA, E, R>
<I, A>(tag: Tag<I, A>, f: (value: A) => A): <XA, E, R>(self: Effect<XA, E, R>) => Effect<XA, E, R | I>
<XA, E, R, I, A>(self: Effect<XA, E, R>, tag: Reference<I, A>, f: (value: A) => A): Effect<XA, E, R>
<XA, E, R, I, A>(self: Effect<XA, E, R>, tag: Tag<I, A>, f: (value: A) => A): Effect<XA, E, R | I>
} = core.updateService
/**
* The `provideService` function is used to provide an actual
* implementation for a service in the context of an effect.
*
* This function allows you to associate a service with its implementation so
* that it can be used in your program. You define the service (e.g., a random
* number generator), and then you use `provideService` to link that
* service to its implementation. Once the implementation is provided, the
* effect can be run successfully without further requirements.
*
* @see {@link provide} for providing multiple layers to an effect.
*
* @example
* ```ts
* import { Effect, Context } from "effect"
*
* // Declaring a tag for a service that generates random numbers
* class Random extends Context.Tag("MyRandomService")<
* Random,
* { readonly next: Effect.Effect<number> }
* >() {}
*
* // Using the service
* const program = Effect.gen(function* () {
* const random = yield* Random
* const randomNumber = yield* random.next
* console.log(`random number: ${randomNumber}`)
* })
*
* // Providing the implementation
* //
* // ┌─── Effect<void, never, never>
* // ▼
* const runnable = Effect.provideService(program, Random, {
* next: Effect.sync(() => Math.random())
* })
*
* // Run successfully
* Effect.runPromise(runnable)
* // Example Output:
* // random number: 0.8241872233134417
* ```
*
* @since 2.0.0
* @category Context
*/
export const provideService: {
<I, S>(tag: Tag<I, S>, service: S): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, Exclude<R, I>>
<A, E, R, I, S>(self: Effect<A, E, R>, tag: Tag<I, S>, service: S): Effect<A, E, Exclude<R, I>>
} = core.provideService
/**
* Provides the effect with the single service it requires. If the effect
* requires more than one service use `provide` instead.
*
* @since 2.0.0
* @category Context
*/
export const provideServiceEffect: {
<I, S, E2, R2>(
tag: Tag<I, S>,
acquire: Effect<S, E2, R2>
): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E | E2, Exclude<R, I> | R2>
<A, E, R, I, S, E2, R2>(
self: Effect<A, E, R>,
tag: Tag<I, S>,
acquire: Effect<S, E2, R2>
): Effect<A, E | E2, Exclude<R, I> | R2>
} = core.provideServiceEffect
/**
* @since 4.0.0
* @category Context
*/
export const provideReferenceScoped: <I, S>(tag: Reference<I, S>, service: S) => Effect<void, never, Scope> =
core.provideReferenceScoped
// -----------------------------------------------------------------------------
// References
// -----------------------------------------------------------------------------
/**
* @since 2.0.0
* @category References
*/
export const withConcurrency: {
(concurrency: number | "unbounded"): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, R>
<A, E, R>(self: Effect<A, E, R>, concurrency: number | "unbounded"): Effect<A, E, R>
} = core.withConcurrency
// -----------------------------------------------------------------------------
// Resource management & finalization
// -----------------------------------------------------------------------------
/**
* @since 2.0.0
* @category Resource management & finalization
*/
export const scope: Effect<Scope, never, Scope> = core.scope
/**
* Scopes all resources used in this workflow to the lifetime of the workflow,
* ensuring that their finalizers are run as soon as this workflow completes
* execution, whether by success, failure, or interruption.
*
* @since 2.0.0
* @category scoping, resources & finalization
*/
export const scoped: <A, E, R>(
effect: Effect<A, E, R>
) => Effect<A, E, Exclude<R, Scope>> = core.scoped
/**
* @since 2.0.0
* @category scoping, resources & finalization
*/
export const scopedWith: <A, E, R>(f: (scope: Scope) => Effect<A, E, R>) => Effect<A, E, R> = core.scopedWith
/**
* This function constructs a scoped resource from an `acquire` and `release`
* `Effect` value.
*
* If the `acquire` `Effect` value successfully completes execution, then the
* `release` `Effect` value will be added to the finalizers associated with the
* scope of this `Effect` value, and it is guaranteed to be run when the scope
* is closed.
*
* The `acquire` and `release` `Effect` values will be run uninterruptibly.
* Additionally, the `release` `Effect` value may depend on the `Exit` value
* specified when the scope is closed.
*
* @since 2.0.0
* @category Resource management & finalization
*/
export const acquireRelease: <A, E, R>(
acquire: Effect<A, E, R>,
release: (a: A, exit: Exit<unknown, unknown>) => Effect<unknown>
) => Effect<A, E, R | Scope> = core.acquireRelease
/**
* This function is used to ensure that an `Effect` value that represents the
* acquisition of a resource (for example, opening a file, launching a thread,
* etc.) will not be interrupted, and that the resource will always be released
* when the `Effect` value completes execution.
*
* `acquireUseRelease` does the following:
*
* 1. Ensures that the `Effect` value that acquires the resource will not be
* interrupted. Note that acquisition may still fail due to internal
* reasons (such as an uncaught exception).
* 2. Ensures that the `release` `Effect` value will not be interrupted,
* and will be executed as long as the acquisition `Effect` value
* successfully acquires the resource.
*
* During the time period between the acquisition and release of the resource,
* the `use` `Effect` value will be executed.
*
* If the `release` `Effect` value fails, then the entire `Effect` value will
* fail, even if the `use` `Effect` value succeeds. If this fail-fast behavior
* is not desired, errors produced by the `release` `Effect` value can be caught
* and ignored.
*
* @since 2.0.0
* @category Resource management & finalization
*/
export const acquireUseRelease: <Resource, E, R, A, E2, R2, E3, R3>(
acquire: Effect<Resource, E, R>,
use: (a: Resource) => Effect<A, E2, R2>,
release: (a: Resource, exit: Exit<A, E2>) => Effect<void, E3, R3>
) => Effect<A, E | E2 | E3, R | R2 | R3> = core.acquireUseRelease
/**
* This function adds a finalizer to the scope of the calling `Effect` value.
* The finalizer is guaranteed to be run when the scope is closed, and it may
* depend on the `Exit` value that the scope is closed with.
*
* @since 2.0.0
* @category Resource management & finalization
*/
export const addFinalizer: (
finalizer: (exit: Exit<unknown, unknown>) => Effect<void>
) => Effect<void, never, Scope> = core.addFinalizer
/**
* Returns an effect that, if this effect _starts_ execution, then the
* specified `finalizer` is guaranteed to be executed, whether this effect
* succeeds, fails, or is interrupted.
*
* For use cases that need access to the effect's result, see `onExit`.
*
* Finalizers offer very powerful guarantees, but they are low-level, and
* should generally not be used for releasing resources. For higher-level
* logic built on `ensuring`, see the `acquireRelease` family of methods.
*
* @since 2.0.0
* @category Resource management & finalization
*/
export const ensuring: {
<X, R1>(
finalizer: Effect<X, never, R1>
): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, R1 | R>
<A, E, R, X, R1>(
self: Effect<A, E, R>,
finalizer: Effect<X, never, R1>
): Effect<A, E, R1 | R>
} = core.ensuring
/**
* Runs the specified effect if this effect fails, providing the error to the
* effect if it exists. The provided effect will not be interrupted.
*
* @since 2.0.0
* @category Resource management & finalization
*/
export const onError: {
<E, X, R2>(
cleanup: (cause: Cause<E>) => Effect<X, never, R2>
): <A, R>(self: Effect<A, E, R>) => Effect<A, E, R2 | R>
<A, E, R, X, R2>(
self: Effect<A, E, R>,
cleanup: (cause: Cause<E>) => Effect<X, never, R2>
): Effect<A, E, R2 | R>
} = core.onError
/**
* Ensures that a cleanup functions runs, whether this effect succeeds, fails,
* or is interrupted.
*
* @since 2.0.0
* @category Resource management & finalization
*/
export const onExit: {
<A, E, X, R2>(
cleanup: (exit: Exit<A, E>) => Effect<X, never, R2>
): <R>(self: Effect<A, E, R>) => Effect<A, E, R2 | R>
<A, E, R, X, R2>(
self: Effect<A, E, R>,
cleanup: (exit: Exit<A, E>) => Effect<X, never, R2>
): Effect<A, E, R | R2>
} = core.onExit
// -----------------------------------------------------------------------------
// Caching
// -----------------------------------------------------------------------------
/**
* Returns an effect that lazily computes a result and caches it for subsequent
* evaluations.
*
* **Details**
*
* This function wraps an effect and ensures that its result is computed only
* once. Once the result is computed, it is cached, meaning that subsequent
* evaluations of the same effect will return the cached result without
* re-executing the logic.
*
* **When to Use**
*
* Use this function when you have an expensive or time-consuming operation that
* you want to avoid repeating. The first evaluation will compute the result,
* and all following evaluations will immediately return the cached value,
* improving performance and reducing unnecessary work.
*
* @see {@link cachedWithTTL} for a similar function that includes a
* time-to-live duration for the cached value.
* @see {@link cachedInvalidateWithTTL} for a similar function that includes an
* additional effect for manually invalidating the cached value.
*
* @example
* ```ts
* import { Effect, Console } from "effect"
*
* let i = 1
* const expensiveTask = Effect.promise<string>(() => {
* console.log("expensive task...")
* return new Promise((resolve) => {
* setTimeout(() => {
* resolve(`result ${i++}`)
* }, 100)
* })
* })
*
* const program = Effect.gen(function* () {
* console.log("non-cached version:")
* yield* expensiveTask.pipe(Effect.andThen(Console.log))
* yield* expensiveTask.pipe(Effect.andThen(Console.log))
* console.log("cached version:")
* const cached = yield* Effect.cached(expensiveTask)
* yield* cached.pipe(Effect.andThen(Console.log))
* yield* cached.pipe(Effect.andThen(Console.log))
* })
*
* Effect.runFork(program)
* // Output:
* // non-cached version:
* // expensive task...
* // result 1
* // expensive task...
* // result 2
* // cached version:
* // expensive task...
* // result 3
* // result 3
* ```
*
* @since 2.0.0
* @category Caching
*/
export const cached: <A, E, R>(self: Effect<A, E, R>) => Effect<Effect<A, E>, never, R> = core.cached
/**
* Returns an effect that caches its result for a specified `Duration`,
* known as "timeToLive" (TTL).
*
* **Details**
*
* This function is used to cache the result of an effect for a specified amount
* of time. This means that the first time the effect is evaluated, its result
* is computed and stored.
*
* If the effect is evaluated again within the specified `timeToLive`, the
* cached result will be used, avoiding recomputation.
*
* After the specified duration has passed, the cache expires, and the effect
* will be recomputed upon the next evaluation.
*
* **When to Use**
*
* Use this function when you have an effect that involves costly operations or
* computations, and you want to avoid repeating them within a short time frame.
*
* It's ideal for scenarios where the result of an effect doesn't change
* frequently and can be reused for a specified duration.
*
* By caching the result, you can improve efficiency and reduce unnecessary
* computations, especially in performance-critical applications.
*
* @see {@link cached} for a similar function that caches the result
* indefinitely.
* @see {@link cachedInvalidateWithTTL} for a similar function that includes an
* additional effect for manually invalidating the cached value.
*
* @example
* ```ts
* import { Effect, Console } from "effect"
*
* let i = 1
* const expensiveTask = Effect.promise<string>(() => {
* console.log("expensive task...")
* return new Promise((resolve) => {
* setTimeout(() => {
* resolve(`result ${i++}`)
* }, 100)
* })
* })
*
* const program = Effect.gen(function* () {
* const cached = yield* Effect.cachedWithTTL(expensiveTask, "150 millis")
* yield* cached.pipe(Effect.andThen(Console.log))
* yield* cached.pipe(Effect.andThen(Console.log))
* yield* Effect.sleep("100 millis")
* yield* cached.pipe(Effect.andThen(Console.log))
* })
*
* Effect.runFork(program)
* // Output:
* // expensive task...
* // result 1
* // result 1
* // expensive task...
* // result 2
* ```
*
* @since 2.0.0
* @category Caching
*/
export const cachedWithTTL: {
(timeToLive: DurationInput): <A, E, R>(self: Effect<A, E, R>) => Effect<Effect<A, E>, never, R>
<A, E, R>(self: Effect<A, E, R>, timeToLive: DurationInput): Effect<Effect<A, E>, never, R>
} = core.cachedWithTTL
/**
* Caches an effect's result for a specified duration and allows manual
* invalidation before expiration.
*
* **Details**
*
* This function behaves similarly to {@link cachedWithTTL} by caching the
* result of an effect for a specified period of time. However, it introduces an
* additional feature: it provides an effect that allows you to manually
* invalidate the cached result before it naturally expires.
*
* This gives you more control over the cache, allowing you to refresh the
* result when needed, even if the original cache has not yet expired.
*
* Once the cache is invalidated, the next time the effect is evaluated, the
* result will be recomputed, and the cache will be refreshed.
*
* **When to Use**
*
* Use this function when you have an effect whose result needs to be cached for
* a certain period, but you also want the option to refresh the cache manually
* before the expiration time.
*
* This is useful when you need to ensure that the cached data remains valid for
* a certain period but still want to invalidate it if the underlying data
* changes or if you want to force a recomputation.
*
* @see {@link cached} for a similar function that caches the result
* indefinitely.
* @see {@link cachedWithTTL} for a similar function that caches the result for
* a specified duration but does not include an effect for manual invalidation.
*
* @example
* ```ts
* import { Effect, Console } from "effect"
*
* let i = 1
* const expensiveTask = Effect.promise<string>(() => {
* console.log("expensive task...")
* return new Promise((resolve) => {
* setTimeout(() => {
* resolve(`result ${i++}`)
* }, 100)
* })
* })
*
* const program = Effect.gen(function* () {
* const [cached, invalidate] = yield* Effect.cachedInvalidateWithTTL(
* expensiveTask,
* "1 hour"
* )
* yield* cached.pipe(Effect.andThen(Console.log))
* yield* cached.pipe(Effect.andThen(Console.log))
* yield* invalidate
* yield* cached.pipe(Effect.andThen(Console.log))
* })
*
* Effect.runFork(program)
* // Output:
* // expensive task...
* // result 1
* // result 1
* // expensive task...
* // result 2
* ```
*
* @since 2.0.0
* @category Caching
*/
export const cachedInvalidateWithTTL: {
(timeToLive: DurationInput): <A, E, R>(
self: Effect<A, E, R>
) => Effect<[Effect<A, E>, Effect<void>], never, R>
<A, E, R>(
self: Effect<A, E, R>,
timeToLive: DurationInput
): Effect<[Effect<A, E>, Effect<void>], never, R>
} = core.cachedInvalidateWithTTL
// -----------------------------------------------------------------------------
// Interruption
// -----------------------------------------------------------------------------
/**
* @since 2.0.0
* @category Interruption
*/
export const interrupt: Effect<never> = core.interrupt
/**
* @since 2.0.0
* @category Interruption
*/
export const interruptible: <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, R> = core.interruptible
/**
* @since 2.0.0
* @category Interruption
*/
export const onInterrupt: {
<XE, XR>(finalizer: Effect<void, XE, XR>): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E | XE, R | XR>
<A, E, R, XE, XR>(self: Effect<A, E, R>, finalizer: Effect<void, XE, XR>): Effect<A, E | XE, R | XR>
} = core.onInterrupt
/**
* @since 2.0.0
* @category Interruption
*/
export const uninterruptible: <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, R> = core.uninterruptible
/**
* @since 2.0.0
* @category Interruption
*/
export const uninterruptibleMask: <A, E, R>(
f: (restore: <AX, EX, RX>(effect: Effect<AX, EX, RX>) => Effect<AX, EX, RX>) => Effect<A, E, R>
) => Effect<A, E, R> = core.uninterruptibleMask
// -----------------------------------------------------------------------------
// Semaphore
// -----------------------------------------------------------------------------
/**
* @category Semaphore
* @since 2.0.0
*/
export interface Semaphore {
/** when the given amount of permits are available, run the effect and release the permits when finished */
withPermits(
permits: number
): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, R>
/** only if the given permits are available, run the effect and release the permits when finished */
withPermitsIfAvailable(
permits: number
): <A, E, R>(self: Effect<A, E, R>) => Effect<Option.Option<A>, E, R>
/** take the given amount of permits, suspending if they are not yet available */
take(permits: number): Effect<number>
/** release the given amount of permits, and return the resulting available permits */
release(permits: number): Effect<number>
/** release all the taken permits, and return the resulting available permits */
releaseAll: Effect<number>
}
/**
* Unsafely creates a new Semaphore
*
* @since 2.0.0
* @category Semaphore
*/
export const unsafeMakeSemaphore: (permits: number) => Semaphore = core.unsafeMakeSemaphore
/**
* Creates a new Semaphore
*
* @since 2.0.0
* @category Semaphore
*/
export const makeSemaphore: (permits: number) => Effect<Semaphore> = core.makeSemaphore
// -----------------------------------------------------------------------------
// Latch
// -----------------------------------------------------------------------------
/**
* @category Latch
* @since 3.8.0
*/
export interface Latch {
/** open the latch, releasing all fibers waiting on it */
readonly open: Effect<boolean>
/** open the latch, releasing all fibers waiting on it */
readonly unsafeOpen: () => boolean
/** release all fibers waiting on the latch, without opening it */
readonly release: Effect<boolean>
/** wait for the latch to be opened */
readonly await: Effect<void>
/** close the latch */
readonly close: Effect<boolean>
/** close the latch */
readonly unsafeClose: () => boolean
/** only run the given effect when the latch is open */
readonly whenOpen: <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, R>
}
/**
* @category Latch
* @since 3.8.0
*/
export const unsafeMakeLatch: (open?: boolean | undefined) => Latch = core.unsafeMakeLatch
/**
* @category latch
* @since 3.8.0
* @example
* ```ts
* import { Effect } from "effect"
*
* Effect.gen(function*() {
* // Create a latch, starting in the closed state
* const latch = yield* Effect.makeLatch(false)
*
* // Fork a fiber that logs "open sesame" when the latch is opened
* const fiber = yield* Effect.log("open sesame").pipe(
* latch.whenOpen,
* Effect.fork
* )
*
* // Open the latch
* yield* latch.open
* yield* fiber.await
* })
* ```
*/
export const makeLatch: (open?: boolean | undefined) => Effect<Latch> = core.makeLatch
// -----------------------------------------------------------------------------
// Repetition & Recursion
// -----------------------------------------------------------------------------
/**
* @since 2.0.0
* @category repetition / recursion
*/
export declare namespace Repeat {
/**
* @since 2.0.0
* @category repetition / recursion
*/
export type Return<R, E, A, O extends Options<A>> = Effect<
(O extends { schedule: Schedule<infer Out, infer _I, infer _R> } ? Out
: O extends { until: Refinement<A, infer B> } ? B
: A),
| E
| (O extends { while: (...args: Array<any>) => Effect<infer _A, infer E, infer _R> } ? E : never)
| (O extends { until: (...args: Array<any>) => Effect<infer _A, infer E, infer _R> } ? E : never),
| R
| (O extends { schedule: Schedule<infer _O, infer _I, infer R> } ? R : never)
| (O extends { while: (...args: Array<any>) => Effect<infer _A, infer _E, infer R> } ? R : never)
| (O extends { until: (...args: Array<any>) => Effect<infer _A, infer _E, infer R> } ? R : never)
> extends infer Z ? Z : never
/**
* @since 2.0.0
* @category repetition / recursion
*/
export interface Options<A> {
while?: ((_: A) => boolean | Effect<boolean, any, any>) | undefined
until?: ((_: A) => boolean | Effect<boolean, any, any>) | undefined
times?: number | undefined
schedule?: Schedule<any, A, any> | undefined
}
}
/**
* Repeats this effect forever (until the first error).
*
* @since 2.0.0
* @category repetition / recursion
*/
export const forever: <
Args extends [self: Effect<any, any, any>, options?: { readonly autoYield?: boolean | undefined }] | [
options?: { readonly autoYield?: boolean | undefined }
]
>(...args: Args) => [Args[0]] extends [Effect<infer _A, infer _E, infer _R>] ? Effect<never, _E, _R>
: <A, E, R>(self: Effect<A, E, R>) => Effect<never, E, R> = core.forever
/**
* Repeats an effect based on a specified schedule or until the first failure.
*
* **Details**
*
* This function executes an effect repeatedly according to the given schedule.
* Each repetition occurs after the initial execution of the effect, meaning
* that the schedule determines the number of additional repetitions. For
* example, using `Schedule.once` will result in the effect being executed twice
* (once initially and once as part of the repetition).
*
* If the effect succeeds, it is repeated according to the schedule. If it
* fails, the repetition stops immediately, and the failure is returned.
*
* The schedule can also specify delays between repetitions, making it useful
* for tasks like retrying operations with backoff, periodic execution, or
* performing a series of dependent actions.
*
* You can combine schedules for more advanced repetition logic, such as adding
* delays, limiting recursions, or dynamically adjusting based on the outcome of
* each execution.
*
* @example
* ```ts
* // Success Example
* import { Effect, Schedule, Console } from "effect"
*
* const action = Console.log("success")
* const policy = Schedule.addDelay(Schedule.recurs(2), () => "100 millis")
* const program = Effect.repeat(action, policy)
*
* // Effect.runPromise(program).then((n) => console.log(`repetitions: ${n}`))
* ```
*
* @example
* // Failure Example
* import { Effect, Schedule } from "effect"
*
* let count = 0
*
* // Define an async effect that simulates an action with possible failures
* const action = Effect.async<string, string>((resume) => {
* if (count > 1) {
* console.log("failure")
* resume(Effect.fail("Uh oh!"))
* } else {
* count++
* console.log("success")
* resume(Effect.succeed("yay!"))
* }
* })
*
* const policy = Schedule.addDelay(Schedule.recurs(2), () => "100 millis")
* const program = Effect.repeat(action, policy)
*
* // Effect.runPromiseExit(program).then(console.log)
*
* @since 2.0.0
* @category repetition / recursion
*/
export const repeat: {
<O extends Repeat.Options<A>, A>(options: O): <E, R>(self: Effect<A, E, R>) => Repeat.Return<R, E, A, O>
<Output, Input, Error, Env>(
schedule: Schedule<Output, Input, NoInfer<Error>, Env>
): <E, R>(self: Effect<Input, E, R>) => Effect<Output, E | Error, R | Env>
<A, E, R, O extends Repeat.Options<A>>(self: Effect<A, E, R>, options: O): Repeat.Return<R, E, A, O>
<Input, E, R, Output, Error, Env>(
self: Effect<Input, E, R>,
schedule: Schedule<Output, Input, NoInfer<Error>, Env>
): Effect<Output, E | Error, R | Env>
} = internalSchedule.repeat
/**
* Repeats an effect with a schedule, handling failures using a custom handler.
*
* **Details**
*
* This function allows you to execute an effect repeatedly based on a specified
* schedule. If the effect fails at any point, a custom failure handler is
* invoked. The handler is provided with both the failure value and the output
* of the schedule at the time of failure. If the effect fails immediately, the
* schedule will never be executed and the output provided to the handler will
* be `None`. This enables advanced error recovery or alternative fallback logic
* while maintaining flexibility in how repetitions are handled.
*
* For example, using a schedule with `recurs(2)` will allow for two additional
* repetitions after the initial execution, provided the effect succeeds. If a
* failure occurs during any iteration, the failure handler is invoked to handle
* the situation.
*
* @example
* ```ts
* import { Effect, Schedule } from "effect"
*
* let count = 0
*
* // Define an async effect that simulates an action with possible failures
* const action = Effect.async<string, string>((resume) => {
* if (count > 1) {
* console.log("failure")
* resume(Effect.fail("Uh oh!"))
* } else {
* count++
* console.log("success")
* resume(Effect.succeed("yay!"))
* }
* })
*
* const policy = Schedule.addDelay(
* Schedule.recurs(2), // Repeat for a maximum of 2 times
* () => "100 millis" // Add a delay of 100 milliseconds between repetitions
* )
*
* const program = Effect.repeatOrElse(action, policy, () =>
* Effect.sync(() => {
* console.log("orElse")
* return count - 1
* })
* )
*
* // Effect.runPromise(program).then((n) => console.log(`repetitions: ${n}`))
* ```
*
* @since 2.0.0
* @category repetition / recursion
*/
export const repeatOrElse: {
<R2, A, B, E, E2, E3, R3>(
schedule: Schedule<B, A, E2, R2>,
orElse: (error: E | E2, option: 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.Option<B>) => Effect<B, E3, R3>
): Effect<B, E3, R | R2 | R3>
} = internalSchedule.repeatOrElse
/**
* Repeats an effect based on a specified schedule.
*
* **Details**
*
* This function allows you to execute an effect repeatedly according to a given
* schedule. The schedule determines the timing and number of repetitions. Each
* repetition can also depend on the decision of the schedule, providing
* flexibility for complex workflows. This function does not modify the effect's
* success or failure; it only controls its repetition.
*
* For example, you can use a schedule that recurs a specific number of times,
* adds delays between repetitions, or customizes repetition behavior based on
* external inputs. The effect runs initially and is repeated according to the
* schedule.
*
* @see {@link scheduleFrom} for a variant that allows the schedule's decision
* to depend on the result of this effect.
*
* @since 2.0.0
* @category Repetition / Recursion
*/
export const schedule = dual<
<Output, Error, Env>(
schedule: Schedule<Output, unknown, Error, Env>
) => <A, E, R>(
self: Effect<A, E, R>
) => Effect<Output, E, R | Env>,
<A, E, R, Output, Error, Env>(
self: Effect<A, E, R>,
schedule: Schedule<Output, unknown, Error, Env>
) => Effect<Output, E, R | Env>
>(2, (self, schedule) => scheduleFrom(self, undefined, schedule))
/**
* Runs an effect repeatedly according to a schedule, starting from a specified
* initial input value.
*
* **Details**
*
* This function allows you to repeatedly execute an effect based on a schedule.
* The schedule starts with the given `initial` input value, which is passed to
* the first execution. Subsequent executions of the effect are controlled by
* the schedule's rules, using the output of the previous iteration as the input
* for the next one.
*
* The returned effect will complete when the schedule ends or the effect fails,
* propagating the error.
*
* @since 2.0.0
* @category Repetition / Recursion
*/
export const scheduleFrom: {
<Input, Output, Error, Env>(
initial: Input,
schedule: Schedule<Output, Input, Error, Env>
): <E, R>(
self: Effect<Input, E, R>
) => Effect<Output, E, R | Env>
<Input, E, R, Output, Error, Env>(
self: Effect<Input, E, R>,
initial: Input,
schedule: Schedule<Output, Input, Error, Env>
): Effect<Output, E, R | Env>
} = internalSchedule.scheduleFrom
// -----------------------------------------------------------------------------
// Tracing
// -----------------------------------------------------------------------------
/**
* @since 2.0.0
* @category Tracing
*/
export const tracer: Effect<Tracer> = core.tracer
/**
* @since 2.0.0
* @category Tracing
*/
export const withTracer: {
(value: Tracer): <A, E, R>(effect: Effect<A, E, R>) => Effect<A, E, R>
<A, E, R>(effect: Effect<A, E, R>, value: Tracer): Effect<A, E, R>
} = core.withTracer
/**
* @since 2.0.0
* @category Tracing
*/
export const withTracerScoped: (value: Tracer) => Effect<void, never, Scope> = core.withTracerScoped
/**
* Disable the tracer for the given Effect.
*
* @since 2.0.0
* @category Tracing
* @example
* ```ts
* import { Effect } from "effect"
*
* Effect.succeed(42).pipe(
* Effect.withSpan("my-span"),
* // the span will not be registered with the tracer
* Effect.withTracerEnabled(false)
* )
* ```
*/
export const withTracerEnabled: {
(enabled: boolean): <A, E, R>(effect: Effect<A, E, R>) => Effect<A, E, R>
<A, E, R>(effect: Effect<A, E, R>, enabled: boolean): Effect<A, E, R>
} = core.withTracerEnabled
/**
* Adds an annotation to each span in this effect.
*
* @since 2.0.0
* @category Tracing
*/
export const annotateSpans: {
(key: string, value: unknown): <A, E, R>(effect: Effect<A, E, R>) => Effect<A, E, R>
(values: Record<string, unknown>): <A, E, R>(effect: Effect<A, E, R>) => Effect<A, E, R>
<A, E, R>(effect: Effect<A, E, R>, key: string, value: unknown): Effect<A, E, R>
<A, E, R>(effect: Effect<A, E, R>, values: Record<string, unknown>): Effect<A, E, R>
} = core.annotateSpans
/**
* Adds an annotation to the current span if available
*
* @since 2.0.0
* @category Tracing
*/
export const annotateCurrentSpan: {
(key: string, value: unknown): Effect<void>
(values: Record<string, unknown>): Effect<void>
} = core.annotateCurrentSpan
/**
* @since 2.0.0
* @category Tracing
*/
export const currentSpan: Effect<Span, NoSuchElementError> = core.currentSpan
/**
* @since 2.0.0
* @category Tracing
*/
export const currentParentSpan: Effect<AnySpan, NoSuchElementError> = core.currentParentSpan
/**
* @since 2.0.0
* @category Tracing
*/
export const spanAnnotations: Effect<Readonly<Record<string, unknown>>> = core.spanAnnotations
/**
* @since 2.0.0
* @category Tracing
*/
export const spanLinks: Effect<ReadonlyArray<SpanLink>> = core.spanLinks
/**
* For all spans in this effect, add a link with the provided span.
*
* @since 2.0.0
* @category Tracing
*/
export const linkSpans: {
(
span: AnySpan | ReadonlyArray<AnySpan>,
attributes?: Record<string, unknown>
): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, R>
<A, E, R>(
self: Effect<A, E, R>,
span: AnySpan | ReadonlyArray<AnySpan>,
attributes?: Record<string, unknown>
): Effect<A, E, R>
} = core.linkSpans
/**
* Create a new span for tracing.
*
* @since 2.0.0
* @category Tracing
*/
export const makeSpan: (
name: string,
options?: SpanOptions
) => Effect<Span> = core.makeSpan
/**
* Create a new span for tracing, and automatically close it when the Scope
* finalizes.
*
* The span is not added to the current span stack, so no child spans will be
* created for it.
*
* @since 2.0.0
* @category Tracing
*/
export const makeSpanScoped: (
name: string,
options?: SpanOptions | undefined
) => Effect<Span, never, Scope> = core.makeSpanScoped
/**
* Create a new span for tracing, and automatically close it when the effect
* completes.
*
* The span is not added to the current span stack, so no child spans will be
* created for it.
*
* @since 2.0.0
* @category Tracing
*/
export const useSpan: {
<A, E, R>(name: string, evaluate: (span: Span) => Effect<A, E, R>): Effect<A, E, R>
<A, E, R>(
name: string,
options: SpanOptions,
evaluate: (span: Span) => Effect<A, E, R>
): Effect<A, E, R>
} = core.useSpan
/**
* Wraps the effect with a new span for tracing.
*
* @since 2.0.0
* @category Tracing
*/
export const withSpan: {
(
name: string,
options?: SpanOptions | undefined
): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, Exclude<R, ParentSpan>>
<A, E, R>(
self: Effect<A, E, R>,
name: string,
options?: SpanOptions | undefined
): Effect<A, E, Exclude<R, ParentSpan>>
} = core.withSpan
/**
* Wraps the effect with a new span for tracing.
*
* The span is ended when the Scope is finalized.
*
* @since 2.0.0
* @category Tracing
*/
export const withSpanScoped: {
(
name: string,
options?: SpanOptions
): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, Exclude<R, ParentSpan> | Scope>
<A, E, R>(
self: Effect<A, E, R>,
name: string,
options?: SpanOptions
): Effect<A, E, Exclude<R, ParentSpan> | Scope>
} = core.withSpanScoped
/**
* Adds the provided span to the current span stack.
*
* @since 2.0.0
* @category Tracing
*/
export const withParentSpan: {
(value: AnySpan): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, Exclude<R, ParentSpan>>
<A, E, R>(self: Effect<A, E, R>, value: AnySpan): Effect<A, E, Exclude<R, ParentSpan>>
} = core.withParentSpan
// -----------------------------------------------------------------------------
// Batching
// -----------------------------------------------------------------------------
/**
* @since 2.0.0
* @category requests & batching
*/
export const request: {
<A extends Request<any, any, any>, EX = never, RX = never>(
resolver: RequestResolver<A> | Effect<RequestResolver<A>, EX, RX>
): (self: A) => Effect<Request.Success<A>, Request.Error<A> | EX, Request.Context<A> | RX>
<A extends Request<any, any, any>, EX = never, RX = never>(
self: A,
resolver: RequestResolver<A> | Effect<RequestResolver<A>, EX, RX>
): Effect<Request.Success<A>, Request.Error<A> | EX, Request.Context<A> | RX>
} = internalRequest.request
// -----------------------------------------------------------------------------
// Supervision & Fiber's
// -----------------------------------------------------------------------------
/**
* Returns an effect that forks this effect into its own separate fiber,
* returning the fiber immediately, without waiting for it to begin executing
* the effect.
*
* You can use the `fork` method whenever you want to execute an effect in a
* new fiber, concurrently and without "blocking" the fiber executing other
* effects. Using fibers can be tricky, so instead of using this method
* directly, consider other higher-level methods, such as `raceWith`,
* `zipPar`, and so forth.
*
* The fiber returned by this method has methods to interrupt the fiber and to
* wait for it to finish executing the effect. See `Fiber` for more
* information.
*
* Whenever you use this method to launch a new fiber, the new fiber is
* attached to the parent fiber's scope. This means when the parent fiber
* terminates, the child fiber will be terminated as well, ensuring that no
* fibers leak. This behavior is called "auto supervision", and if this
* behavior is not desired, you may use the `forkDaemon` or `forkIn` methods.
*
* @since 2.0.0
* @category supervision & fibers
*/
export const fork: <A, E, R>(
self: Effect<A, E, R>
) => Effect<Fiber<A, E>, never, R> = core.fork
/**
* Forks the effect in the specified scope. The fiber will be interrupted
* when the scope is closed.
*
* @since 2.0.0
* @category supervision & fibers
*/
export const forkIn: {
(
scope: Scope
): <A, E, R>(self: Effect<A, E, R>) => Effect<Fiber<A, E>, never, R>
<A, E, R>(self: Effect<A, E, R>, scope: Scope): Effect<Fiber<A, E>, never, R>
} = core.forkIn
/**
* Forks the fiber in a `Scope`, interrupting it when the scope is closed.
*
* @since 2.0.0
* @category supervision & fibers
*/
export const forkScoped: <A, E, R>(
self: Effect<A, E, R>
) => Effect<Fiber<A, E>, never, Scope | R> = core.forkScoped
/**
* Forks the effect into a new fiber attached to the global scope. Because the
* new fiber is attached to the global scope, when the fiber executing the
* returned effect terminates, the forked fiber will continue running.
*
* @since 2.0.0
* @category supervision & fibers
*/
export const forkDaemon: <A, E, R>(self: Effect<A, E, R>) => Effect<Fiber<A, E>, never, R> = core.forkDaemon
// -----------------------------------------------------------------------------
// Running Effects
// -----------------------------------------------------------------------------
/**
* The foundational function for running effects, returning a "fiber" that can
* be observed or interrupted.
*
* **When to Use**
*
* `runFork` is used to run an effect in the background by creating a
* fiber. It is the base function for all other run functions. It starts a fiber
* that can be observed or interrupted.
*
* Unless you specifically need a `Promise` or synchronous operation,
* `runFork` is a good default choice.
*
* @example
* ```ts
* // Title: Running an Effect in the Background
* import { Effect, Console, Schedule, Fiber } from "effect"
*
* // ┌─── Effect<number, never, never>
* // ▼
* const program = Effect.repeat(
* Console.log("running..."),
* Schedule.spaced("200 millis")
* )
*
* // ┌─── RuntimeFiber<number, never>
* // ▼
* const fiber = Effect.runFork(program)
*
* setTimeout(() => {
* Effect.runFork(Fiber.interrupt(fiber))
* }, 500)
* ```
*
* @since 2.0.0
* @category Running Effects
*/
export const runFork: <A, E>(
effect: Effect<A, E>,
options?:
| {
readonly signal?: AbortSignal | undefined
readonly scheduler?: Scheduler | undefined
}
| undefined
) => Fiber<A, E> = core.runFork
/**
* Executes an effect and returns the result as a `Promise`.
*
* **When to Use**
*
* Use `runPromise` when you need to execute an effect and work with the
* result using `Promise` syntax, typically for compatibility with other
* promise-based code.
*
* If the effect succeeds, the promise will resolve with the result. If the
* effect fails, the promise will reject with an error.
*
* @see {@link runPromiseExit} for a version that returns an `Exit` type instead of rejecting.
*
* @example
* ```ts
* // Title: Running a Successful Effect as a Promise
* import { Effect } from "effect"
*
* Effect.runPromise(Effect.succeed(1)).then(console.log)
* // Output: 1
* ```
*
* @example
* //Example: Handling a Failing Effect as a Rejected Promise
* import { Effect } from "effect"
*
* Effect.runPromise(Effect.fail("my error")).catch(console.error)
* // Output:
* // (FiberFailure) Error: my error
*
* @since 2.0.0
* @category Running Effects
*/
export const runPromise: <A, E>(
effect: Effect<A, E>,
options?:
| {
readonly signal?: AbortSignal | undefined
readonly scheduler?: Scheduler | undefined
}
| undefined
) => Promise<A> = core.runPromise
/**
* Runs an effect and returns a `Promise` that resolves to an `Exit`, which
* represents the outcome (success or failure) of the effect.
*
* **When to Use**
*
* Use `runPromiseExit` when you need to determine if an effect succeeded
* or failed, including any defects, and you want to work with a `Promise`.
*
* **Details**
*
* The `Exit` type represents the result of the effect:
* - If the effect succeeds, the result is wrapped in a `Success`.
* - If it fails, the failure information is provided as a `Failure` containing
* a `Cause` type.
*
* @example
* ```ts
* // Title: Handling Results as Exit
* import { Effect } from "effect"
*
* // Execute a successful effect and get the Exit result as a Promise
* Effect.runPromiseExit(Effect.succeed(1)).then(console.log)
* // Output:
* // {
* // _id: "Exit",
* // _tag: "Success",
* // value: 1
* // }
*
* // Execute a failing effect and get the Exit result as a Promise
* Effect.runPromiseExit(Effect.fail("my error")).then(console.log)
* // Output:
* // {
* // _id: "Exit",
* // _tag: "Failure",
* // cause: {
* // _id: "Cause",
* // _tag: "Fail",
* // failure: "my error"
* // }
* // }
* ```
*
* @since 2.0.0
* @category Running Effects
*/
export const runPromiseExit: <A, E>(
effect: Effect<A, E>,
options?:
| {
readonly signal?: AbortSignal | undefined
readonly scheduler?: Scheduler | undefined
}
| undefined
) => Promise<Exit<A, E>> = core.runPromiseExit
/**
* Executes an effect synchronously, running it immediately and returning the
* result.
*
* **When to Use**
*
* Use `runSync` to run an effect that does not fail and does not include
* any asynchronous operations.
*
* If the effect fails or involves asynchronous work, it will throw an error,
* and execution will stop where the failure or async operation occurs.
*
* @see {@link runSyncExit} for a version that returns an `Exit` type instead of
* throwing an error.
*
* @example
* ```ts
* // Title: Synchronous Logging
* import { Effect } from "effect"
*
* const program = Effect.sync(() => {
* console.log("Hello, World!")
* return 1
* })
*
* const result = Effect.runSync(program)
* // Output: Hello, World!
*
* console.log(result)
* // Output: 1
* ```
*
* @example
* // Title: Incorrect Usage with Failing or Async Effects
* import { Effect } from "effect"
*
* try {
* // Attempt to run an effect that fails
* Effect.runSync(Effect.fail("my error"))
* } catch (e) {
* console.error(e)
* }
* // Output:
* // (FiberFailure) Error: my error
*
* try {
* // Attempt to run an effect that involves async work
* Effect.runSync(Effect.promise(() => Promise.resolve(1)))
* } catch (e) {
* console.error(e)
* }
* // Output:
* // (FiberFailure) AsyncFiberException: Fiber #0 cannot be resolved synchronously. This is caused by using runSync on an effect that performs async work
*
* @since 2.0.0
* @category Running Effects
*/
export const runSync: <A, E>(effect: Effect<A, E>) => A = core.runSync
/**
* Runs an effect synchronously and returns the result as an `Exit` type, which
* represents the outcome (success or failure) of the effect.
*
* **When to Use**
*
* Use `runSyncExit` to find out whether an effect succeeded or failed,
* including any defects, without dealing with asynchronous operations.
*
* **Details**
*
* The `Exit` type represents the result of the effect:
* - If the effect succeeds, the result is wrapped in a `Success`.
* - If it fails, the failure information is provided as a `Failure` containing
* a `Cause` type.
*
* If the effect contains asynchronous operations, `runSyncExit` will
* return an `Failure` with a `Die` cause, indicating that the effect cannot be
* resolved synchronously.
*
* @example
* ```ts
* // Title: Handling Results as Exit
* import { Effect } from "effect"
*
* console.log(Effect.runSyncExit(Effect.succeed(1)))
* // Output:
* // {
* // _id: "Exit",
* // _tag: "Success",
* // value: 1
* // }
*
* console.log(Effect.runSyncExit(Effect.fail("my error")))
* // Output:
* // {
* // _id: "Exit",
* // _tag: "Failure",
* // cause: {
* // _id: "Cause",
* // _tag: "Fail",
* // failure: "my error"
* // }
* // }
* ```
*
* @example
* // Title: Asynchronous Operation Resulting in Die
* import { Effect } from "effect"
*
* console.log(Effect.runSyncExit(Effect.promise(() => Promise.resolve(1))))
* // Output:
* // {
* // _id: 'Exit',
* // _tag: 'Failure',
* // cause: {
* // _id: 'Cause',
* // _tag: 'Die',
* // defect: [Fiber #0 cannot be resolved synchronously. This is caused by using runSync on an effect that performs async work] {
* // fiber: [FiberRuntime],
* // _tag: 'AsyncFiberException',
* // name: 'AsyncFiberException'
* // }
* // }
* // }
*
* @since 2.0.0
* @category Running Effects
*/
export const runSyncExit: <A, E>(effect: Effect<A, E>) => Exit<A, E> = core.runSyncExit
// -----------------------------------------------------------------------------
// Function
// -----------------------------------------------------------------------------
/**
* @since 3.12.0
* @category Function
*/
export namespace fn {
/**
* @since 3.12.0
* @category Function
*/
export interface Gen {
<
Eff extends YieldWrap<Effect<any, any, any>>,
AEff,
Args extends Array<any>
>(
body: (...args: Args) => Generator<Eff, AEff, never>
): (
...args: Args
) => Effect<
AEff,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer E, infer _R>>] ? E
: never,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer _E, infer R>>] ? R
: never
>
<
Eff extends YieldWrap<Effect<any, any, any>>,
AEff,
Args extends Array<any>,
A extends Effect<any, any, any>
>(
body: (...args: Args) => Generator<Eff, AEff, never>,
a: (
_: Effect<
AEff,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer E, infer _R>>] ? E
: never,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer _E, infer R>>] ? R
: never
>
) => A
): (...args: Args) => A
<
Eff extends YieldWrap<Effect<any, any, any>>,
AEff,
Args extends Array<any>,
A,
B extends Effect<any, any, any>
>(
body: (...args: Args) => Generator<Eff, AEff, never>,
a: (
_: Effect<
AEff,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer E, infer _R>>] ? E
: never,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer _E, infer R>>] ? R
: never
>
) => A,
b: (_: A) => B
): (...args: Args) => B
<
Eff extends YieldWrap<Effect<any, any, any>>,
AEff,
Args extends Array<any>,
A,
B,
C extends Effect<any, any, any>
>(
body: (...args: Args) => Generator<Eff, AEff, never>,
a: (
_: Effect<
AEff,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer E, infer _R>>] ? E
: never,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer _E, infer R>>] ? R
: never
>
) => A,
b: (_: A) => B,
c: (_: B) => C
): (...args: Args) => C
<
Eff extends YieldWrap<Effect<any, any, any>>,
AEff,
Args extends Array<any>,
A,
B,
C,
D extends Effect<any, any, any>
>(
body: (...args: Args) => Generator<Eff, AEff, never>,
a: (
_: Effect<
AEff,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer E, infer _R>>] ? E
: never,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer _E, infer R>>] ? R
: never
>
) => A,
b: (_: A) => B,
c: (_: B) => C,
d: (_: C) => D
): (...args: Args) => D
<
Eff extends YieldWrap<Effect<any, any, any>>,
AEff,
Args extends Array<any>,
A,
B,
C,
D,
E extends Effect<any, any, any>
>(
body: (...args: Args) => Generator<Eff, AEff, never>,
a: (
_: Effect<
AEff,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer E, infer _R>>] ? E
: never,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer _E, infer R>>] ? R
: never
>
) => A,
b: (_: A) => B,
c: (_: B) => C,
d: (_: C) => D,
e: (_: D) => E
): (...args: Args) => E
<
Eff extends YieldWrap<Effect<any, any, any>>,
AEff,
Args extends Array<any>,
A,
B,
C,
D,
E,
F extends Effect<any, any, any>
>(
body: (...args: Args) => Generator<Eff, AEff, never>,
a: (
_: Effect<
AEff,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer E, infer _R>>] ? E
: never,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer _E, infer R>>] ? R
: never
>
) => A,
b: (_: A) => B,
c: (_: B) => C,
d: (_: C) => D,
e: (_: D) => E,
f: (_: E) => F
): (...args: Args) => F
<
Eff extends YieldWrap<Effect<any, any, any>>,
AEff,
Args extends Array<any>,
A,
B,
C,
D,
E,
F,
G extends Effect<any, any, any>
>(
body: (...args: Args) => Generator<Eff, AEff, never>,
a: (
_: Effect<
AEff,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer E, infer _R>>] ? E
: never,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer _E, infer R>>] ? R
: never
>
) => A,
b: (_: A) => B,
c: (_: B) => C,
d: (_: C) => D,
e: (_: D) => E,
f: (_: E) => F,
g: (_: F) => G
): (...args: Args) => G
<
Eff extends YieldWrap<Effect<any, any, any>>,
AEff,
Args extends Array<any>,
A,
B,
C,
D,
E,
F,
G,
H extends Effect<any, any, any>
>(
body: (...args: Args) => Generator<Eff, AEff, never>,
a: (
_: Effect<
AEff,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer E, infer _R>>] ? E
: never,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer _E, infer R>>] ? R
: never
>
) => A,
b: (_: A) => B,
c: (_: B) => C,
d: (_: C) => D,
e: (_: D) => E,
f: (_: E) => F,
g: (_: F) => G,
h: (_: G) => H
): (...args: Args) => H
<
Eff extends YieldWrap<Effect<any, any, any>>,
AEff,
Args extends Array<any>,
A,
B,
C,
D,
E,
F,
G,
H,
I extends Effect<any, any, any>
>(
body: (...args: Args) => Generator<Eff, AEff, never>,
a: (
_: Effect<
AEff,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer E, infer _R>>] ? E
: never,
[Eff] extends [never] ? never
: [Eff] extends [YieldWrap<Effect<infer _A, infer _E, infer R>>] ? R
: never
>
) => A,
b: (_: A) => B,
c: (_: B) => C,
d: (_: C) => D,
e: (_: D) => E,
f: (_: E) => F,
g: (_: F) => G,
h: (_: G) => H,
i: (_: H) => I
): (...args: Args) => I
}
/**
* @since 3.11.0
* @category models
*/
export interface NonGen {
<Eff extends Effect<any, any, any>, Args extends Array<any>>(
body: (...args: Args) => Eff
): (...args: Args) => Eff
<Eff extends Effect<any, any, any>, A, Args extends Array<any>>(
body: (...args: Args) => A,
a: (_: A) => Eff
): (...args: Args) => Eff
<Eff extends Effect<any, any, any>, A, B, Args extends Array<any>>(
body: (...args: Args) => A,
a: (_: A) => B,
b: (_: B) => Eff
): (...args: Args) => Eff
<Eff extends Effect<any, any, any>, A, B, C, Args extends Array<any>>(
body: (...args: Args) => A,
a: (_: A) => B,
b: (_: B) => C,
c: (_: C) => Eff
): (...args: Args) => Eff
<Eff extends Effect<any, any, any>, A, B, C, D, Args extends Array<any>>(
body: (...args: Args) => A,
a: (_: A) => B,
b: (_: B) => C,
c: (_: C) => D,
d: (_: D) => Eff
): (...args: Args) => Eff
<Eff extends Effect<any, any, any>, A, B, C, D, E, Args extends Array<any>>(
body: (...args: Args) => A,
a: (_: A) => B,
b: (_: B) => C,
c: (_: C) => D,
d: (_: D) => E,
e: (_: E) => Eff
): (...args: Args) => Eff
<
Eff extends Effect<any, any, any>,
A,
B,
C,
D,
E,
F,
Args extends Array<any>
>(
body: (...args: Args) => A,
a: (_: A) => B,
b: (_: B) => C,
c: (_: C) => D,
d: (_: D) => E,
e: (_: E) => F,
f: (_: E) => Eff
): (...args: Args) => Eff
<
Eff extends Effect<any, any, any>,
A,
B,
C,
D,
E,
F,
G,
Args extends Array<any>
>(
body: (...args: Args) => A,
a: (_: A) => B,
b: (_: B) => C,
c: (_: C) => D,
d: (_: D) => E,
e: (_: E) => F,
f: (_: E) => G,
g: (_: G) => Eff
): (...args: Args) => Eff
<
Eff extends Effect<any, any, any>,
A,
B,
C,
D,
E,
F,
G,
H,
Args extends Array<any>
>(
body: (...args: Args) => A,
a: (_: A) => B,
b: (_: B) => C,
c: (_: C) => D,
d: (_: D) => E,
e: (_: E) => F,
f: (_: E) => G,
g: (_: G) => H,
h: (_: H) => Eff
): (...args: Args) => Eff
<
Eff extends Effect<any, any, any>,
A,
B,
C,
D,
E,
F,
G,
H,
I,
Args extends Array<any>
>(
body: (...args: Args) => A,
a: (_: A) => B,
b: (_: B) => C,
c: (_: C) => D,
d: (_: D) => E,
e: (_: E) => F,
f: (_: E) => G,
g: (_: G) => H,
h: (_: H) => I,
i: (_: H) => Eff
): (...args: Args) => Eff
}
}
/**
* Creates a function that returns an Effect.
*
* The function can be created using a generator function that can yield
* effects.
*
* `Effect.fnUntraced` also acts as a `pipe` function, allowing you to create a pipeline after the function definition.
*
* @example
* ```ts
* // Title: Creating a traced function with a generator function
* import { Effect } from "effect"
*
* const logExample = Effect.fnUntraced(function*<N extends number>(n: N) {
* yield* Effect.annotateCurrentSpan("n", n)
* yield* Effect.logInfo(`got: ${n}`)
* yield* Effect.fail(new Error())
* })
*
* Effect.runFork(logExample(100))
* ```
*
* @since 3.12.0
* @category function
*/
export const fnUntraced: fn.Gen = core.fnUntraced
// ========================================================================
// Logging
// ========================================================================
/**
* @since 2.0.0
* @category logging
*/
export const log: (...message: ReadonlyArray<any>) => Effect<void> = core.logWithLevel()
/**
* @since 2.0.0
* @category logging
*/
export const logFatal: (...message: ReadonlyArray<any>) => Effect<void> = core.logWithLevel("Fatal")
/**
* @since 2.0.0
* @category logging
*/
export const logWarning: (...message: ReadonlyArray<any>) => Effect<void> = core.logWithLevel("Warning")
/**
* @since 2.0.0
* @category logging
*/
export const logError: (...message: ReadonlyArray<any>) => Effect<void> = core.logWithLevel("Error")
/**
* @since 2.0.0
* @category logging
*/
export const logInfo: (...message: ReadonlyArray<any>) => Effect<void> = core.logWithLevel("Info")
/**
* @since 2.0.0
* @category logging
*/
export const logDebug: (...message: ReadonlyArray<any>) => Effect<void> = core.logWithLevel("Debug")
/**
* @since 2.0.0
* @category logging
*/
export const logTrace: (...message: ReadonlyArray<any>) => Effect<void> = core.logWithLevel("Trace")
/**
* Adds a logger to the set of loggers which will output logs for this effect.
*
* @since 2.0.0
* @category logging
*/
export const withLogger = dual<
<Output>(logger: Logger<unknown, Output>) => <A, E, R>(effect: Effect<A, E, R>) => Effect<A, E, R>,
<A, E, R, Output>(effect: Effect<A, E, R>, logger: Logger<unknown, Output>) => Effect<A, E, R>
>(2, (effect, logger) => core.updateService(effect, core.CurrentLoggers, (loggers) => new Set([...loggers, logger])))
/**
* Adds an annotation to each log line in this effect.
*
* @since 2.0.0
* @category logging
*/
export const annotateLogs = dual<
{
(key: string, value: unknown): <A, E, R>(effect: Effect<A, E, R>) => Effect<A, E, R>
(values: Record<string, unknown>): <A, E, R>(effect: Effect<A, E, R>) => Effect<A, E, R>
},
{
<A, E, R>(effect: Effect<A, E, R>, key: string, value: unknown): Effect<A, E, R>
<A, E, R>(effect: Effect<A, E, R>, values: Record<string, unknown>): Effect<A, E, R>
}
>(
(args) => core.isEffect(args[0]),
<A, E, R>(
effect: Effect<A, E, R>,
...args: [Record<string, unknown>] | [key: string, value: unknown]
): Effect<A, E, R> =>
core.updateService(effect, CurrentLogAnnotations, (annotations) => {
const newAnnotations = { ...annotations }
if (args.length === 1) {
Object.assign(newAnnotations, args[0])
} else {
newAnnotations[args[0]] = args[1]
}
return newAnnotations
})
)
/**
* Adds a span to each log line in this effect.
*
* @since 2.0.0
* @category logging
*/
export const withLogSpan = dual<
(label: string) => <A, E, R>(effect: Effect<A, E, R>) => Effect<A, E, R>,
<A, E, R>(effect: Effect<A, E, R>, label: string) => Effect<A, E, R>
>(
2,
(effect, label) =>
core.flatMap(core.currentTimeMillis, (now) =>
core.updateService(effect, CurrentLogSpans, (spans) => {
const span: [label: string, timestamp: number] = [label, now]
return [span, ...spans]
}))
)