<A>(evaluate: LazyArg<A>): Effect.Effect<ScopedRef<A>, never, Scope.Scope>Creates a new ScopedRef from the specified value.
When to use
Use to create a ScopedRef when the initial value is already available or
can be produced without acquiring resources.
Details
The evaluate function runs when the returned effect runs. The returned
effect requires a Scope, and the reference closes the currently stored
value's scope when that outer scope closes.
Gotchas
Do not use make for an initial value whose creation acquires resources; use
fromAcquire so acquisition and finalization are tracked.
export const const make: <A>(
evaluate: LazyArg<A>
) => Effect.Effect<
ScopedRef<A>,
never,
Scope.Scope
>
Creates a new ScopedRef from the specified value.
When to use
Use to create a ScopedRef when the initial value is already available or
can be produced without acquiring resources.
Details
The evaluate function runs when the returned effect runs. The returned
effect requires a Scope, and the reference closes the currently stored
value's scope when that outer scope closes.
Gotchas
Do not use make for an initial value whose creation acquires resources; use
fromAcquire so acquisition and finalization are tracked.
make = <function (type parameter) A in <A>(evaluate: LazyArg<A>): Effect.Effect<ScopedRef<A>, never, Scope.Scope>A>(evaluate: LazyArg<A>evaluate: type LazyArg<A> = () => AA zero-argument function that produces a value when invoked.
When to use
Use to type a lazy value provider that should not run until called.
Example (Creating a lazy argument)
import { Function } from "effect"
const constNull: Function.LazyArg<null> = Function.constant(null)
LazyArg<function (type parameter) A in <A>(evaluate: LazyArg<A>): Effect.Effect<ScopedRef<A>, never, Scope.Scope>A>): 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>(evaluate: LazyArg<A>): Effect.Effect<ScopedRef<A>, never, Scope.Scope>A>, never, import ScopeScope.Scope> =>
import EffectEffect.const suspend: <A, E, R>(
effect: LazyArg<Effect<A, E, R>>
) => Effect<A, E, R>
Creates an Effect lazily, delaying construction until it is needed.
When to use
Use when you need to defer the evaluation of an effect until it is required.
Details
suspend takes a thunk that represents an effect and delays creating it
until the suspended effect is evaluated. This is useful for optimizing
expensive computations, managing circular dependencies such as recursive
functions, and helping TypeScript unify return types when branches construct
different effects. Any side effects or scoped captures inside the thunk are
re-executed on each invocation.
Example (Lazily evaluating 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 (Suspending recursive Fibonacci evaluation)
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 (Helping TypeScript infer recursive effect 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)
)
suspend(() => {
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 = evaluate: LazyArg<A>evaluate()
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)
return import EffectEffect.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>
}
as(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)), 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)
})