<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>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.
Details
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.
Example (Always running cleanup)
import { Console, Effect } from "effect"
const task = Effect.gen(function*() {
yield* Console.log("Task started")
yield* Effect.sleep("1 second")
yield* Console.log("Task completed")
return 42
})
// Ensure cleanup always runs, regardless of success or failure
const program = Effect.ensuring(
task,
Console.log("Cleanup: This always runs!")
)
Effect.runPromise(program).then(console.log)
// Output:
// Task started
// Task completed
// Cleanup: This always runs!
// 42export const 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>
}
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.
Details
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.
Example (Always running cleanup)
import { Console, Effect } from "effect"
const task = Effect.gen(function*() {
yield* Console.log("Task started")
yield* Effect.sleep("1 second")
yield* Console.log("Task completed")
return 42
})
// Ensure cleanup always runs, regardless of success or failure
const program = Effect.ensuring(
task,
Console.log("Cleanup: This always runs!")
)
Effect.runPromise(program).then(console.log)
// Output:
// Task started
// Task completed
// Cleanup: This always runs!
// 42
ensuring: {
<function (type parameter) X in <X, R1>(finalizer: Effect<X, never, R1>): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, R1 | R>X, function (type parameter) R1 in <X, R1>(finalizer: Effect<X, never, R1>): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, R1 | R>R1>(
finalizer: Effect<X, never, R1>(parameter) finalizer: {
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;
}
finalizer: 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) X in <X, R1>(finalizer: Effect<X, never, R1>): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, R1 | R>X, never, function (type parameter) R1 in <X, R1>(finalizer: Effect<X, never, R1>): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, R1 | R>R1>
): <function (type parameter) A in <A, E, R>(self: Effect<A, E, R>): Effect<A, E, R1 | R>A, function (type parameter) E in <A, E, R>(self: Effect<A, E, R>): Effect<A, E, R1 | R>E, function (type parameter) R in <A, E, R>(self: Effect<A, E, R>): Effect<A, E, R1 | R>R>(self: Effect<A, E, R>(parameter) self: {
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;
}
self: 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>(self: Effect<A, E, R>): Effect<A, E, R1 | R>A, function (type parameter) E in <A, E, R>(self: Effect<A, E, R>): Effect<A, E, R1 | R>E, function (type parameter) R in <A, E, R>(self: Effect<A, E, R>): Effect<A, E, R1 | R>R>) => 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>(self: Effect<A, E, R>): Effect<A, E, R1 | R>A, function (type parameter) E in <A, E, R>(self: Effect<A, E, R>): Effect<A, E, R1 | R>E, function (type parameter) R1 in <X, R1>(finalizer: Effect<X, never, R1>): <A, E, R>(self: Effect<A, E, R>) => Effect<A, E, R1 | R>R1 | function (type parameter) R in <A, E, R>(self: Effect<A, E, R>): Effect<A, E, R1 | R>R>
<function (type parameter) A in <A, E, R, X, R1>(self: Effect<A, E, R>, finalizer: Effect<X, never, R1>): Effect<A, E, R1 | R>A, function (type parameter) E in <A, E, R, X, R1>(self: Effect<A, E, R>, finalizer: Effect<X, never, R1>): Effect<A, E, R1 | R>E, function (type parameter) R in <A, E, R, X, R1>(self: Effect<A, E, R>, finalizer: Effect<X, never, R1>): Effect<A, E, R1 | R>R, function (type parameter) X in <A, E, R, X, R1>(self: Effect<A, E, R>, finalizer: Effect<X, never, R1>): Effect<A, E, R1 | R>X, function (type parameter) R1 in <A, E, R, X, R1>(self: Effect<A, E, R>, finalizer: Effect<X, never, R1>): Effect<A, E, R1 | R>R1>(
self: Effect<A, E, R>(parameter) self: {
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;
}
self: 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, X, R1>(self: Effect<A, E, R>, finalizer: Effect<X, never, R1>): Effect<A, E, R1 | R>A, function (type parameter) E in <A, E, R, X, R1>(self: Effect<A, E, R>, finalizer: Effect<X, never, R1>): Effect<A, E, R1 | R>E, function (type parameter) R in <A, E, R, X, R1>(self: Effect<A, E, R>, finalizer: Effect<X, never, R1>): Effect<A, E, R1 | R>R>,
finalizer: Effect<X, never, R1>(parameter) finalizer: {
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;
}
finalizer: 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) X in <A, E, R, X, R1>(self: Effect<A, E, R>, finalizer: Effect<X, never, R1>): Effect<A, E, R1 | R>X, never, function (type parameter) R1 in <A, E, R, X, R1>(self: Effect<A, E, R>, finalizer: Effect<X, never, R1>): Effect<A, E, R1 | R>R1>
): 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, X, R1>(self: Effect<A, E, R>, finalizer: Effect<X, never, R1>): Effect<A, E, R1 | R>A, function (type parameter) E in <A, E, R, X, R1>(self: Effect<A, E, R>, finalizer: Effect<X, never, R1>): Effect<A, E, R1 | R>E, function (type parameter) R1 in <A, E, R, X, R1>(self: Effect<A, E, R>, finalizer: Effect<X, never, R1>): Effect<A, E, R1 | R>R1 | function (type parameter) R in <A, E, R, X, R1>(self: Effect<A, E, R>, finalizer: Effect<X, never, R1>): Effect<A, E, R1 | R>R>
} = import internalinternal.const ensuring: {
<XE, XR>(
finalizer: Effect.Effect<void, XE, XR>
): <A, E, R>(
self: Effect.Effect<A, E, R>
) => Effect.Effect<A, E | XE, R | XR>
<A, E, R, XE, XR>(
self: Effect.Effect<A, E, R>,
finalizer: Effect.Effect<void, XE, XR>
): Effect.Effect<A, E | XE, R | XR>
}
ensuring