Exit
Represents the result of an Effect computation as a plain value.
An Exit<A, E> is either a success with an A or a failure with a
Cause<E>. The failure cause preserves typed errors, defects, and
interruptions after a workflow has finished. Use this module when completed
Effect results need to be inspected, transformed, filtered, or matched
synchronously as data.
Combinators
Discards the success value of an Exit, replacing it with void.
When to use
Use when you need to discard a successful Exit value while preserving
whether the Exit succeeded or failed.
Details
Failures pass through unchanged.
Allocates a new Exit if successful.
See
Signature
declare const asVoid: <A, E>(self: Exit<A, E>) => Exit<void, E>Example
(Discarding the success value)
import { Exit } from "effect"
Exit.asVoid(Exit.succeed(42)) // => Exit.succeed(undefined)Combines multiple Exit values into a single Exit<void, E>.
When to use
Use to validate that all exits in a collection succeeded
Details
If all exits are successful, this returns a void success. If any exit is a failure, this returns a single failure with all error causes combined.
Iterates over the entire collection. Collects all failure causes, not just the first.
See
- asVoid to discard the value of a single Exit
Signature
declare const asVoidAll: <I extends Iterable<Exit<any, any>>>(exits: I) => Exit<void, I extends Iterable<Exit<infer _A, infer _E>> ? _E : never>Example
(Combining exits)
import { Exit } from "effect"
Exit.asVoidAll([Exit.succeed(1), Exit.succeed(2), Exit.succeed(3)]) // => Exit.succeed(undefined)Exit.asVoidAll([Exit.succeed(1), Exit.fail("err"), Exit.succeed(3)]) // => Exit.fail("err")Transforms the success value of an Exit using the given function.
When to use
Use to apply a transformation to the value inside a successful Exit
Details
Failures pass through unchanged.
Allocates a new Exit if successful.
See
Signature
declare const map: { <A, B>(f: (a: A) => B): <E>(self: Exit<A, E>) => Exit<B, E>; <A, E, B>(self: Exit<A, E>, f: (a: A) => B): Exit<B, E>;}Example
(Mapping over a success)
import { Exit } from "effect"
Exit.map(Exit.succeed(21), (x) => x * 2) // => Exit.succeed(42)Transforms both the success value and typed error of an Exit.
When to use
Use when you need to remap both channels in one step.
Details
onSuccess transforms the value if the Exit is a Success. onFailure
transforms the typed error if the Exit is a Failure with a Fail reason.
Allocates a new Exit.
Gotchas
If the Cause contains only defects or interruptions, the failure passes through unchanged.
See
Signature
declare const mapBoth: { <E, E2, A, A2>(options: { readonly onFailure: (e: E) => E2; readonly onSuccess: (a: A) => A2; }): (self: Exit<A, E>) => Exit<A2, E2>; <A, E, E2, A2>(self: Exit<A, E>, options: { readonly onFailure: (e: E) => E2; readonly onSuccess: (a: A) => A2; }): Exit<A2, E2>;}Example
(Mapping both channels)
import { Exit } from "effect"
Exit.mapBoth(Exit.succeed(42), { onSuccess: (x) => String(x), onFailure: (error: string) => error.toUpperCase()}) // => Exit.succeed("42")Transforms the typed error of a failed Exit using the given function.
When to use
Use to remap typed errors while preserving the Exit structure
Details
Successes pass through unchanged.
Allocates a new Exit if the error is transformed.
Gotchas
Only transforms typed errors (Fail reasons). If the Cause contains only defects or interruptions, the failure passes through unchanged.
See
Signature
declare const mapError: { <E, E2>(f: (a: NoInfer<E>) => E2): <A>(self: Exit<A, E>) => Exit<A, E2>; <A, E, E2>(self: Exit<A, E>, f: (a: NoInfer<E>) => E2): Exit<A, E2>;}Example
(Mapping over an error)
import { Exit } from "effect"
Exit.mapError(Exit.fail("bad input"), (error) => error.toUpperCase()) // => Exit.fail("BAD INPUT")Constructors
Creates a failed Exit from a defect (unexpected error).
When to use
Use when you need unexpected, unrecoverable errors that should not appear in the typed error channel.
Details
The defect is wrapped in a Cause.Die internally.
Returns a Failure<never> with E = never, since defects do not appear in
the typed error channel.
See
Signature
declare const die: (defect: unknown) => Exit<never>Example
(Creating a defect Exit)
import { Exit } from "effect"
Exit.die("Unexpected error") // => Exit.die("Unexpected error")Creates a failed Exit from a typed error value.
When to use
Use when you need to represent an expected typed failure as an Exit.
Details
The error is wrapped in a Cause.Fail internally.
Returns a Failure<never, E>.
See
Signature
declare const fail: <E>(e: E) => Exit<never, E>Example
(Creating a failed Exit)
import { Exit } from "effect"
Exit.fail("Something went wrong") // => Exit.fail("Something went wrong")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.
See
Signature
declare const failCause: <E>(cause: Cause.Cause<E>) => Exit<never, E>Example
(Creating a failed Exit from a Cause)
import { Cause, Exit } from "effect"
Exit.failCause(Cause.fail("Something went wrong")) // => Exit.fail("Something went wrong")Creates a failed Exit representing fiber interruption.
When to use
Use to signal that a fiber was interrupted.
Details
Optionally pass a fiber ID to identify which fiber was interrupted. Returns
a Failure<never> with an Interrupt cause.
See
- hasInterrupts to check whether an Exit contains interruptions
Signature
declare const interrupt: (fiberId?: number) => Exit<never>Example
(Creating an interruption Exit)
import { Exit } from "effect"
Exit.interrupt(123) // => Exit.interrupt(123)Creates a successful Exit containing the given value.
When to use
Use when you need an Exit that contains a known success value.
Details
Returns a Success<A> with the provided value. Does not perform any
computation.
See
Signature
declare const succeed: <A>(a: A) => Exit<A>Example
(Creating a successful Exit)
import { Exit } from "effect"
Exit.succeed(42) // => Exit.succeed(42)Filtering
filterCause
Extracts the Cause from a failed Exit as a Result.
When to use
Use when composing Exit checks with Filter or other Result-based
filtering APIs and you want the raw Cause rather than the Failure wrapper.
Details
Returns Result.succeed(cause) when the Exit is a Failure, or
Result.fail(success) with the original Success otherwise.
Gotchas
This is not an Option accessor or an Effect failure. A failed extraction is
represented as data in the Result failure channel.
See
- filterFailure to get the full Failure object
- getCause to get the Cause as an Option instead
Signature
declare const filterCause: <A, E>(self: Exit<A, E>) => Result.Result<Cause.Cause<E>, Success<A>>Example
(Filtering for the cause)
import { Cause, Exit, Result } from "effect"
Exit.filterCause(Exit.fail("err")) // => Result.succeed(Cause.fail("err"))filterFailure
Extracts the Failure variant from an Exit as a Result.
When to use
Use when composing Exit checks with Filter or other Result-based
filtering APIs and you want the full Failure wrapper.
Details
Returns Result.succeed(failure) when the Exit is a Failure, or
Result.fail(success) with the original Success otherwise.
Gotchas
This is not an Option accessor or an Effect failure. A failed extraction is
represented as data in the Result failure channel.
See
- filterSuccess for the inverse
- filterCause to extract the Cause directly
Signature
declare const filterFailure: <A, E>(self: Exit<A, E>) => Result.Result<Failure<never, E>, Success<A>>Example
(Filtering for failure)
import { Exit, Result } from "effect"
Exit.filterFailure(Exit.fail("err")) // => Result.succeed(Exit.fail("err"))filterSuccess
Extracts the Success variant from an Exit as a Result.
When to use
Use when composing Exit checks with Filter or other Result-based
filtering APIs and you want the full Success wrapper.
Details
Returns Result.succeed(success) when the Exit is a Success, or
Result.fail(failure) with the original Failure otherwise.
Gotchas
This is not an Option accessor or an Effect failure. A failed extraction is
represented as data in the Result failure channel.
See
- filterFailure for the inverse
- filterValue to extract the raw value instead of the Success object
Signature
declare const filterSuccess: <A, E>(self: Exit<A, E>) => Result.Result<Success<A>, Failure<never, E>>Example
(Filtering for success)
import { Exit, Result } from "effect"
Exit.filterSuccess(Exit.succeed(42)) // => Result.succeed(Exit.succeed(42))filterValue
Extracts the success value from an Exit as a Result.
When to use
Use when composing Exit checks with Filter or other Result-based
filtering APIs and you want the raw success value rather than the Success
wrapper.
Details
Returns Result.succeed(value) when the Exit is a Success, or
Result.fail(failure) with the original Failure otherwise.
Gotchas
This is not an Option accessor or an Effect failure. A failed extraction is
represented as data in the Result failure channel.
See
- filterSuccess to get the full Success object
- getSuccess to get the value as an Option instead
Signature
declare const filterValue: <A, E>(self: Exit<A, E>) => Result.Result<A, Failure<never, E>>Example
(Filtering for the value)
import { Exit, Result } from "effect"
Exit.filterValue(Exit.succeed(42)) // => Result.succeed(42)findDefect
Extracts the first defect from a failed Exit as a Result.
When to use
Use when you need the first defect from an Exit as a Result for
Filter or other Result-based filtering APIs.
Details
Returns Result.succeed(defect) when the Cause contains a Die reason, or
Result.fail(exit) with the original Exit otherwise.
Gotchas
Only finds the first Die reason. If the Cause has multiple defects, the rest are ignored.
See
Signature
declare const findDefect: <A, E>(input: Exit<A, E>) => Result.Result<unknown, Exit<A, E>>Example
(Finding the first defect)
import { Exit, Result } from "effect"
Exit.findDefect(Exit.die("boom")) // => Result.succeed("boom")Exit.findDefect(Exit.fail("err")) // => Result.fail(Exit.fail("err"))Extracts the first typed error value from a failed Exit as a Result.
When to use
Use when you need the first typed error from an Exit as a Result for
Filter or other Result-based filtering APIs.
Details
Returns Result.succeed(error) when the Cause contains a Fail reason, or
Result.fail(exit) with the original Exit otherwise.
Gotchas
Only finds the first Fail reason. If the Cause has multiple errors, the rest are ignored.
See
- findErrorOption to get the error as an Option instead
- findDefect to find defects instead
Signature
declare const findError: <A, E>(input: Exit<A, E>) => Result.Result<E, Exit<A, E>>Example
(Finding the first typed error)
import { Exit, Result } from "effect"
Exit.findError(Exit.fail("not found")) // => Result.succeed("not found")Exit.findError(Exit.die("bug")) // => Result.fail(Exit.die("bug"))Getters
findErrorOption
Returns the first typed error from a failed Exit as an Option.
When to use
Use when you need the first typed error from an Exit as an Option,
ignoring successes and non-typed failures.
Details
Returns Option.some(error) if the Cause contains a Fail reason. Successes,
defect-only failures, and interrupt-only failures return Option.none().
Gotchas
Only finds the first Fail reason. If the Cause has multiple typed errors, the rest are ignored.
See
Signature
declare const findErrorOption: <A, E>(self: Exit<A, E>) => Option<E>Example
(Getting the first error)
import { Exit, Option } from "effect"
Exit.findErrorOption(Exit.fail("err")) // => Option.some("err")Exit.findErrorOption(Exit.die("bug")) // => Option.none()Exit.findErrorOption(Exit.succeed(42)) // => Option.none()Returns the Cause of a failed Exit as an Option.
When to use
Use when you need the failure Cause from an Exit as an Option instead
of pattern matching.
Details
Returns Option.some(cause) for a Failure and Option.none() for a Success.
See
- getSuccess to extract the success value
- filterCause for filter-pipeline usage
Signature
declare const getCause: <A, E>(self: Exit<A, E>) => Option<Cause.Cause<E>>Example
(Getting the failure cause)
import { Cause, Exit, Option } from "effect"
Exit.getCause(Exit.fail("err")) // => Option.some(Cause.fail("err"))Exit.getCause(Exit.succeed(42)) // => Option.none()getSuccess
Returns the success value of an Exit as an Option.
When to use
Use when you need the success value from an Exit as an Option instead of
pattern matching.
Details
Returns Option.some(value) for a Success and Option.none() for a Failure.
See
- getCause to extract the Cause of a failure
- filterValue for filter-pipeline usage
Signature
declare const getSuccess: <A, E>(self: Exit<A, E>) => Option<A>Example
(Getting the success value)
import { Exit, Option } from "effect"
Exit.getSuccess(Exit.succeed(42)) // => Option.some(42)Exit.getSuccess(Exit.fail("err")) // => Option.none()Guards
Checks whether a failed Exit contains defects (Die reasons).
When to use
Use to check whether an Exit failure cause contains unexpected errors.
Details
Returns false for successful exits. Only checks for Die reasons in the
Cause. A Cause with only Fail or Interrupt reasons returns false.
See
- hasFails to check for typed errors
- hasInterrupts to check for interruptions
Signature
declare const hasDies: <A, E>(self: Exit<A, E>) => self is Failure<A, E>Example
(Checking for defects)
import { Exit } from "effect"
Exit.hasDies(Exit.die("bug")) // => trueExit.hasDies(Exit.fail("err")) // => falseExit.hasDies(Exit.succeed(42)) // => falseChecks whether a failed Exit contains typed errors (Fail reasons).
When to use
Use to distinguish typed failures from defects or interruptions.
Details
Returns false for successful exits. Only checks for Fail reasons in the
Cause. A Cause with only Die or Interrupt reasons returns false.
See
- hasDies to check for defects
- hasInterrupts to check for interruptions
Signature
declare const hasFails: <A, E>(self: Exit<A, E>) => self is Failure<A, E>Example
(Checking for typed errors)
import { Exit } from "effect"
Exit.hasFails(Exit.fail("err")) // => trueExit.hasFails(Exit.die("bug")) // => falseExit.hasFails(Exit.succeed(42)) // => falsehasInterrupts
Checks whether a failed Exit contains interruptions (Interrupt reasons).
When to use
Use to check whether an Exit contains fiber interruption.
Details
Returns false for successful exits. Only checks for Interrupt reasons in
the Cause. A Cause with only Fail or Die reasons returns false.
See
Signature
declare const hasInterrupts: <A, E>(self: Exit<A, E>) => self is Failure<A, E>Example
(Checking for interruptions)
import { Exit } from "effect"
Exit.hasInterrupts(Exit.interrupt(1)) // => trueExit.hasInterrupts(Exit.fail("err")) // => falseExit.hasInterrupts(Exit.succeed(42)) // => falseChecks whether an unknown value is an Exit.
When to use
Use to validate unknown values at system boundaries and narrow them to
Exit<unknown, unknown>.
Details
Does not inspect the contents of the Exit. Returns true for both Success
and Failure exits.
See
Signature
declare const isExit: (u: unknown) => u is Exit<unknown, unknown>Example
(Checking if a value is an Exit)
import { Exit } from "effect"
Exit.isExit(Exit.succeed(42)) // => trueExit.isExit(Exit.fail("err")) // => trueExit.isExit("not an exit") // => falseChecks whether an Exit is a Failure.
When to use
Use as a type guard to narrow Exit<A, E> to Failure<A, E> and access the
cause property.
See
Signature
declare const isFailure: <A, E>(self: Exit<A, E>) => self is Failure<A, E>Example
(Narrowing to failure)
import { Cause, Exit } from "effect"
const exit = Exit.fail("error")
if (Exit.isFailure(exit)) { exit.cause // => Cause.fail("error")}Checks whether an Exit is a Success.
When to use
Use as a type guard to narrow Exit<A, E> to Success<A, E> and access the
value property.
See
Signature
declare const isSuccess: <A, E>(self: Exit<A, E>) => self is Success<A, E>Example
(Narrowing to success)
import { Exit } from "effect"
const exit = Exit.succeed(42)
if (Exit.isSuccess(exit)) { exit.value // => 42}Models
Represents the result of an Effect computation.
When to use
Use when you need to synchronously inspect whether an Effect computation succeeded or failed.
Details
An Exit<A, E> is either Success<A, E> containing a value of type A, or
Failure<A, E> containing a Cause<E> describing why the computation
failed.
Since Exit is also an Effect, you can yield it inside Effect.gen.
See
Signature
type Exit<A, E = never> = Success<A, E> | Failure<A, E>Example
(Pattern matching on an Exit)
import { Exit } from "effect"
const success: Exit.Exit<number> = Exit.succeed(42)const failure: Exit.Exit<number, string> = Exit.fail("error")
Exit.match(success, { onSuccess: (value) => `Got value: ${value}`, onFailure: (cause) => `Got error: ${cause}`}) // => "Got value: 42"A failed Exit containing a Cause.
When to use
Use when working with the failed branch of an Exit after narrowing with
isFailure. Access the cause via the cause property after
narrowing.
Details
The Cause<E> may contain typed errors, defects, or interruptions.
See
Signature
interface Failure<out A, out E> extends Proto<A, E> { readonly _tag: "Failure"; readonly cause: Cause<E>;}Example
(Accessing the failure cause)
import { Cause, Exit } from "effect"
const failure = Exit.fail("something went wrong")
if (Exit.isFailure(failure)) { failure.cause // => Cause.fail("something went wrong")}A successful Exit containing a value.
When to use
Use when working with the successful branch of an Exit after narrowing
with isSuccess. Access the value via the value property after
narrowing.
See
Signature
interface Success<out A, out E = never> extends Proto<A, E> { readonly _tag: "Success"; readonly value: A;}Example
(Accessing the success value)
import { Exit } from "effect"
const success = Exit.succeed(42)
if (Exit.isSuccess(success)) { success.value // => 42}Other
Pattern Matching
Pattern matches on an Exit, handling both success and failure cases.
When to use
Use when you need exhaustive handling of both Exit success and failure
outcomes.
Details
Calls onSuccess with the value if the Exit is a Success, and calls
onFailure with the Cause if the Exit is a Failure.
See
Signature
declare const match: { <A, E, X1, X2>(options: { readonly onFailure: (cause: Cause.Cause<NoInfer<E>>) => X2; readonly onSuccess: (a: NoInfer<A>) => X1; }): (self: Exit<A, E>) => X1 | X2; <A, E, X1, X2>(self: Exit<A, E>, options: { readonly onFailure: (cause: Cause.Cause<E>) => X2; readonly onSuccess: (a: A) => X1; }): X1 | X2;}Example
(Matching on an Exit)
import { Exit } from "effect"
Exit.match(Exit.succeed(42), { onSuccess: (value) => `Got: ${value}`, onFailure: () => "Failed"}) // => "Got: 42"