<A, E, R>(acquire: Effect.Effect<A, E, R>): Effect.Effect<
ScopedRef<A>,
E,
Scope.Scope | R
>Creates a new ScopedRef from an effect that acquires the initial value.
When to use
Use when creating a ScopedRef whose initial value requires acquiring
resources that must be released.
export const const fromAcquire: <A, E, R>(
acquire: Effect.Effect<A, E, R>
) => Effect.Effect<
ScopedRef<A>,
E,
Scope.Scope | R
>
Creates a new ScopedRef from an effect that acquires the initial value.
When to use
Use when creating a ScopedRef whose initial value requires acquiring
resources that must be released.
fromAcquire: <function (type parameter) A in <A, E, R>(acquire: Effect.Effect<A, E, R>): Effect.Effect<ScopedRef<A>, E, Scope.Scope | R>A, function (type parameter) E in <A, E, R>(acquire: Effect.Effect<A, E, R>): Effect.Effect<ScopedRef<A>, E, Scope.Scope | R>E, function (type parameter) R in <A, E, R>(acquire: Effect.Effect<A, E, R>): Effect.Effect<ScopedRef<A>, E, Scope.Scope | R>R>(
acquire: Effect.Effect<A, E, R>(parameter) acquire: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
acquire: import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<function (type parameter) A in <A, E, R>(acquire: Effect.Effect<A, E, R>): Effect.Effect<ScopedRef<A>, E, Scope.Scope | R>A, function (type parameter) E in <A, E, R>(acquire: Effect.Effect<A, E, R>): Effect.Effect<ScopedRef<A>, E, Scope.Scope | R>E, function (type parameter) R in <A, E, R>(acquire: Effect.Effect<A, E, R>): Effect.Effect<ScopedRef<A>, E, Scope.Scope | R>R>
) => import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<interface ScopedRef<in out A>A ScopedRef is a reference whose value is associated with resources,
which must be released properly. You can both get the current value of any
ScopedRef, as well as set it to a new value (which may require new
resources). The reference itself takes care of properly releasing resources
for the old value whenever a new value is obtained.
When to use
Use when an application needs to keep a current resource-backed value and
later replace it with another acquired value while ensuring the previous
value is released.
ScopedRef<function (type parameter) A in <A, E, R>(acquire: Effect.Effect<A, E, R>): Effect.Effect<ScopedRef<A>, E, Scope.Scope | R>A>, function (type parameter) E in <A, E, R>(acquire: Effect.Effect<A, E, R>): Effect.Effect<ScopedRef<A>, E, Scope.Scope | R>E, import ScopeScope.Scope | function (type parameter) R in <A, E, R>(acquire: Effect.Effect<A, E, R>): Effect.Effect<ScopedRef<A>, E, Scope.Scope | R>R> = import EffectEffect.const fnUntraced: <Effect.Effect<void, never, Scope.Scope> | Effect.Effect<A, E, Exclude<R, Scope.Scope>>, ScopedRef<A>, [acquire: Effect.Effect<A, E, R>], Effect.Effect<ScopedRef<A>, E, Scope.Scope | Exclude<R, Scope.Scope>>>(body: (this: unassigned, acquire: Effect.Effect<A, E, R>) => Generator<Effect.Effect<void, never, Scope.Scope> | Effect.Effect<A, E, Exclude<R, Scope.Scope>>, ScopedRef<...>, never>, a: (_: Effect.Effect<...>, acquire: Effect.Effect<...>) => Effect.Effect<...>) => (acquire: Effect.Effect<...>) => Effect.Effect<...> (+41 overloads)fnUntraced(function*<function (type parameter) A in <A, E, R>(acquire: Effect.Effect<A, E, R>): Generator<Effect.Effect<A, E, Exclude<R, Scope.Scope>> | Effect.Effect<void, never, Scope.Scope>, ScopedRef<...>, any>A, function (type parameter) E in <A, E, R>(acquire: Effect.Effect<A, E, R>): Generator<Effect.Effect<A, E, Exclude<R, Scope.Scope>> | Effect.Effect<void, never, Scope.Scope>, ScopedRef<...>, any>E, function (type parameter) R in <A, E, R>(acquire: Effect.Effect<A, E, R>): Generator<Effect.Effect<A, E, Exclude<R, Scope.Scope>> | Effect.Effect<void, never, Scope.Scope>, ScopedRef<...>, any>R>(
acquire: Effect.Effect<A, E, R>(parameter) acquire: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
acquire: import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<function (type parameter) A in <A, E, R>(acquire: Effect.Effect<A, E, R>): Generator<Effect.Effect<A, E, Exclude<R, Scope.Scope>> | Effect.Effect<void, never, Scope.Scope>, ScopedRef<...>, any>A, function (type parameter) E in <A, E, R>(acquire: Effect.Effect<A, E, R>): Generator<Effect.Effect<A, E, Exclude<R, Scope.Scope>> | Effect.Effect<void, never, Scope.Scope>, ScopedRef<...>, any>E, function (type parameter) R in <A, E, R>(acquire: Effect.Effect<A, E, R>): Generator<Effect.Effect<A, E, Exclude<R, Scope.Scope>> | Effect.Effect<void, never, Scope.Scope>, ScopedRef<...>, any>R>
) {
const const scope: Scope.Closeableconst scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope = import ScopeScope.const makeUnsafe: (
finalizerStrategy?: "sequential" | "parallel"
) => Closeable
Creates a new Scope synchronously without wrapping it in an Effect.
This is useful when you need a scope immediately but should be used with caution
as it doesn't provide the same safety guarantees as the Effect-wrapped version.
When to use
Use when a scope must be allocated synchronously and the caller will close it
manually.
Example (Creating a scope synchronously)
import { Console, Effect, Exit, Scope } from "effect"
// Create a scope immediately
const scope = Scope.makeUnsafe("sequential")
// Use it in an Effect program
const program = Effect.gen(function*() {
yield* Scope.addFinalizer(scope, Console.log("Cleanup"))
yield* Scope.close(scope, Exit.void)
})
makeUnsafe()
const const value: Avalue = yield* acquire: Effect.Effect<A, E, R>(parameter) acquire: {
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
acquire.Pipeable.pipe<Effect.Effect<A, E, R>, Effect.Effect<A, E, Exclude<R, Scope.Scope>>, Effect.Effect<A, E, Exclude<R, Scope.Scope>>>(this: Effect.Effect<A, E, R>, ab: (_: Effect.Effect<A, E, R>) => Effect.Effect<A, E, Exclude<R, Scope.Scope>>, bc: (_: Effect.Effect<A, E, Exclude<R, Scope.Scope>>) => Effect.Effect<A, E, Exclude<R, Scope.Scope>>): Effect.Effect<...> (+21 overloads)pipe(
import ScopeScope.const provide: {
(value: Scope): <A, E, R>(
self: Effect<A, E, R>
) => Effect<A, E, Exclude<R, Scope>>
<A, E, R>(
self: Effect<A, E, R>,
value: Scope
): Effect<A, E, Exclude<R, Scope>>
}
provide(const scope: Scope.Closeableconst scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope),
import EffectEffect.const tapCause: {
<E, X, E2, R2>(
f: (
cause: 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.Cause<E>
) => Effect<X, E2, R2>
): Effect<A, E | E2, R | R2>
}
tapCause((cause: Cause<NoInfer<E>>(parameter) cause: {
reasons: ReadonlyArray<Reason<E>>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
cause) => import ScopeScope.const close: <A, E>(
self: Scope,
exit: Exit<A, E>
) => Effect<void>
Closes a scope and runs its registered finalizers.
When to use
Use to close a scope manually with a specific exit value.
Details
Finalizers run in the scope's configured order and receive the supplied
Exit.
Example (Running scope finalizers)
import { Console, Effect, Exit, Scope } from "effect"
const resourceManagement = Effect.gen(function*() {
const scope = yield* Scope.make("sequential")
// Add multiple finalizers
yield* Scope.addFinalizer(scope, Console.log("Close database connection"))
yield* Scope.addFinalizer(scope, Console.log("Close file handle"))
yield* Scope.addFinalizer(scope, Console.log("Release memory"))
// Do some work...
yield* Console.log("Performing operations...")
// Close scope - finalizers run in reverse order of registration
yield* Scope.close(scope, Exit.succeed("Success!"))
// Output: "Release memory", "Close file handle", "Close database connection"
})
close(const scope: Scope.Closeableconst scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope, import ExitExit.const failCause: <E>(
cause: Cause.Cause<E>
) => Exit<never, E>
Creates a failed Exit from a Cause.
When to use
Use when you already have a Cause<E> and want to wrap it in an Exit
for advanced error handling where you need full control over the Cause
structure.
Details
Returns a Failure<never, E>. If you only have an error value, use
fail
instead.
Example (Creating a failed Exit from a Cause)
import { Cause, Exit } from "effect"
const cause = Cause.fail("Something went wrong")
const exit = Exit.failCause(cause)
console.log(Exit.isFailure(exit)) // true
failCause(cause: Cause<NoInfer<E>>(parameter) cause: {
reasons: ReadonlyArray<Reason<E>>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
cause)))
)
const const self: ScopedRef<A>const self: {
backing: Synchronized.SynchronizedRef<readonly [Scope.Closeable, A]>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
self = const makeUnsafe: <A>(
scope: Scope.Closeable,
value: A
) => ScopedRef<A>
makeUnsafe(const scope: Scope.Closeableconst scope: {
strategy: "sequential" | "parallel";
state: State.Open | State.Closed | State.Empty;
}
scope, const value: Avalue)
yield* import EffectEffect.const addFinalizer: <R>(
finalizer: (
exit: Exit.Exit<unknown, unknown>
) => Effect<void, never, R>
) => Effect<void, never, R | Scope>
Adds a finalizer to the current scope.
When to use
Use to register low-level cleanup in the current scope.
Details
The finalizer runs when the surrounding scope is closed and receives the
Exit value used to close the scope.
Example (Registering scope finalizers)
import { Console, Effect, Exit } from "effect"
const program = Effect.scoped(
Effect.gen(function*() {
// Add a finalizer that runs when the scope closes
yield* Effect.addFinalizer((exit) =>
Console.log(
Exit.isSuccess(exit)
? "Cleanup: Operation completed successfully"
: "Cleanup: Operation failed, cleaning up resources"
)
)
yield* Console.log("Performing main operation...")
// This could succeed or fail
return "operation result"
})
)
Effect.runPromise(program).then(console.log)
// Output:
// Performing main operation...
// Cleanup: Operation completed successfully
// operation result
addFinalizer((exit: Exit.Exit<unknown, unknown>exit) => import ScopeScope.const close: <A, E>(
self: Scope,
exit: Exit<A, E>
) => Effect<void>
Closes a scope and runs its registered finalizers.
When to use
Use to close a scope manually with a specific exit value.
Details
Finalizers run in the scope's configured order and receive the supplied
Exit.
Example (Running scope finalizers)
import { Console, Effect, Exit, Scope } from "effect"
const resourceManagement = Effect.gen(function*() {
const scope = yield* Scope.make("sequential")
// Add multiple finalizers
yield* Scope.addFinalizer(scope, Console.log("Close database connection"))
yield* Scope.addFinalizer(scope, Console.log("Close file handle"))
yield* Scope.addFinalizer(scope, Console.log("Release memory"))
// Do some work...
yield* Console.log("Performing operations...")
// Close scope - finalizers run in reverse order of registration
yield* Scope.close(scope, Exit.succeed("Success!"))
// Output: "Release memory", "Close file handle", "Close database connection"
})
close(const self: ScopedRef<A>const self: {
backing: Synchronized.SynchronizedRef<readonly [Scope.Closeable, A]>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
self.ScopedRef<A>.backing: Synchronized.SynchronizedRef<readonly [Scope.Closeable, A]>(property) ScopedRef<A>.backing: {
backing: Ref.Ref<A>;
semaphore: Semaphore.Semaphore;
ref: MutableRef.MutableRef<A>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
backing.SynchronizedRef<readonly [Closeable, A]>.backing: Ref.Ref<A>(property) SynchronizedRef<readonly [Closeable, A]>.backing: {
ref: MutableRef.MutableRef<A>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
backing.Ref<readonly [Closeable, A]>.ref: MutableRef.MutableRef<A>(property) Ref<readonly [Closeable, A]>.ref: {
current: T;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
ref.MutableRef<readonly [Closeable, A]>.current: readonly [Scope.Closeable, A](property) MutableRef<readonly [Closeable, A]>.current: {
0: Scope.Closeable;
1: A;
length: 2;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<Scope.Closeable | A>>): Array<Scope.Closeable | A>; (...items: Array<Scope.Closeable | A | ConcatArray<Scope.Closeable | A>>): Array<Scope.Closeable | A> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<Scope.Closeable | A>;
indexOf: (searchElement: Scope.Closeable | A, fromIndex?: number) => number;
lastIndexOf: (searchElement: Scope.Closeable | A, fromIndex?: number) => number;
every: { (predicate: (value: Scope.Closeable | A, index: number, array: ReadonlyArray<Scope.Closeable | A>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: Scope.Closeable | A, index: number, array: ReadonlyArray<Scope.Cl…;
some: (predicate: (value: Scope.Closeable | A, index: number, array: ReadonlyArray<Scope.Closeable | A>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: Scope.Closeable | A, index: number, array: ReadonlyArray<Scope.Closeable | A>) => void, thisArg?: any) => void;
map: (callbackfn: (value: Scope.Closeable | A, index: number, array: ReadonlyArray<Scope.Closeable | A>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: Scope.Closeable | A, index: number, array: ReadonlyArray<Scope.Closeable | A>) => value is S, thisArg?: any): Array<S>; (predicate: (value: Scope.Closeable | A, index: number, array: ReadonlyArray<Scope.Closeable | A>…;
reduce: { (callbackfn: (previousValue: Scope.Closeable | A, currentValue: Scope.Closeable | A, currentIndex: number, array: ReadonlyArray<Scope.Closeable | A>) => Scope.Closeable | A): Scope.Closeable | A; (callbackfn: (previousValue: Scope.Closea…;
reduceRight: { (callbackfn: (previousValue: Scope.Closeable | A, currentValue: Scope.Closeable | A, currentIndex: number, array: ReadonlyArray<Scope.Closeable | A>) => Scope.Closeable | A): Scope.Closeable | A; (callbackfn: (previousValue: Scope.Closea…;
find: { (predicate: (value: Scope.Closeable | A, index: number, obj: ReadonlyArray<Scope.Closeable | A>) => value is S, thisArg?: any): S | undefined; (predicate: (value: Scope.Closeable | A, index: number, obj: ReadonlyArray<Scope.Closeable | A…;
findIndex: (predicate: (value: Scope.Closeable | A, index: number, obj: ReadonlyArray<Scope.Closeable | A>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, Scope.Closeable | A]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<Scope.Closeable | A>;
includes: (searchElement: Scope.Closeable | A, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: Scope.Closeable | A, index: number, array: Array<Scope.Closeable | A>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => Scope.Closeable | A | undefined;
findLast: { (predicate: (value: Scope.Closeable | A, index: number, array: ReadonlyArray<Scope.Closeable | A>) => value is S, thisArg?: any): S | undefined; (predicate: (value: Scope.Closeable | A, index: number, array: ReadonlyArray<Scope.Closeable…;
findLastIndex: (predicate: (value: Scope.Closeable | A, index: number, array: ReadonlyArray<Scope.Closeable | A>) => unknown, thisArg?: any) => number;
toReversed: () => Array<Scope.Closeable | A>;
toSorted: (compareFn?: ((a: Scope.Closeable | A, b: Scope.Closeable | A) => number) | undefined) => Array<Scope.Closeable | A>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<Scope.Closeable | A>): Array<Scope.Closeable | A>; (start: number, deleteCount?: number): Array<Scope.Closeable | A> };
with: (index: number, value: Scope.Closeable | A) => Array<Scope.Closeable | A>;
}
current[0], exit: Exit.Exit<unknown, unknown>exit))
return const self: ScopedRef<A>const self: {
backing: Synchronized.SynchronizedRef<readonly [Scope.Closeable, A]>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
self
}, import EffectEffect.const uninterruptible: <A, E, R>(
self: Effect<A, E, R>
) => Effect<A, E, R>
Returns a new effect that disables interruption for the given effect.
Example (Preventing interruption)
import { Console, Effect, Fiber } from "effect"
const criticalTask = Effect.gen(function*() {
yield* Console.log("Starting critical section...")
yield* Effect.sleep("2 seconds")
yield* Console.log("Critical section completed")
})
const program = Effect.uninterruptible(criticalTask)
const fiber = Effect.runFork(program)
// Even if interrupted, the critical task will complete
Effect.runPromise(Fiber.interrupt(fiber))
uninterruptible)