Skip to content

Exit

Exit.Exit<A, E> represents the complete settled outcome of an Effect: either Success<A> holding a value or Failure<E> holding a full Cause (typed errors, defects, and interruption). Use it to inspect, store, or replay an Effect's result without losing failure information.

OperationBehavior
try exit(program)Runs program and returns its Exit.Exit<A, E> as data
Exit.succeed(a), Exit.failCause(c)Construct constant Exit values without failing
Exit.Success(let v), Exit.Failure(let c)Exhaustive match and is patterns
try outcomeReplays a stored Exit (returns value or propagates cause)
Effect.onExit(program, fn)Runs cleanup receiving program's Exit

Representing full outcomes

error ExportRejected {
  reason: string
}
 
effect function inspectExport() {
  const outcome = try exit(effect {
    throw new ExportRejected({ reason: "empty selection" })
  })
  if (Exit.isFailure(outcome)) return outcome.cause.reasons.length
  return 0
}
// Run in an effect function: try Console.log(try inspectExport())
1

try exit(program) executes program and captures its outcome—Success with .value or Failure with .cause—without propagating the failure into the caller.

Creating success and cause-based failure exits

function sampleOutcomes() {
  const successful = Exit.succeed(12)
  const failed = Exit.failCause(
    Cause.combine(Cause.fail("write rejected"), Cause.die(new Error("release failed")))
  )
  return { successful, failed }
}
 
function constantSuccess() {
  return Exit.succeed(12)
}
// Exit.Exit<number>
// Run in an effect function:
//   const outcomes = sampleOutcomes()
//   try Console.log(Exit.isSuccess(outcomes.successful), Exit.isFailure(outcomes.failed), Exit.isSuccess(constantSuccess()))
//   if (Exit.isFailure(outcomes.failed)) {
//     const tags = outcomes.failed.cause.reasons.map((reason) => reason._tag)
//     try Console.log(try Effect.try(() => JSON.stringify(tags)))
//   }
true true true
["Fail","Die"]

Exit.succeed and Exit.failCause construct immutable outcome data anywhere, including module top level, without raising failures.

Matching exit variants

function describe<A, E>(outcome: Exit.Exit<A, E>) {
  return match (outcome) {
    when Exit.Success(let value): `Completed: ${String(value)}`;
    when Exit.Failure(let cause): Cause.pretty(cause);
  }
}
// Run in an effect function:
//   try Console.log(describe(Exit.succeed(12)))
//   try Console.log(describe(Exit.failCause(Cause.empty)) === "")
Completed: 12
true

match checks exhaustiveness over Exit.Success and Exit.Failure, and its arms can run Effects with try inside effect functions. Use outcome is Exit.Failure to test a single variant, or Exit.match(outcome, { onSuccess, onFailure }) for synchronous callbacks.

Relating exits to results

function asResult<A, E>(outcome: Exit.Exit<A, E>) {
  if (Exit.isSuccess(outcome)) return Result.succeed(outcome.value)
  return Result.fail(outcome.cause)
}
// Result.Result<A, Cause.Cause<E>>
// Run in an effect function:
//   try Console.log(try Effect.try(() => Result.getOrThrow(asResult(Exit.succeed(12)))))
//   const failed = asResult(Exit.failCause(Cause.combine(Cause.fail("rejected"), Cause.die("broken"))))
//   if (Result.isFailure(failed)) try Console.log(try Effect.try(() => JSON.stringify(failed.failure.reasons.map((reason) => reason._tag))))
12
["Fail","Die"]

Exit<A, E> is isomorphic to Result.Result<A, Cause.Cause<E>>; narrowing to Result.Result<A, E> discards defects and interruption. Preserve the Cause whenever storing outcomes in another data structure.

Relating exits to effects

effect function replay<A, E>(outcome: Exit.Exit<A, E>) {
  return try outcome
}
// A!E
// Run in an effect function:
//   try Console.log(try replay(Exit.succeed(12)))
//   const failed = try exit(replay(Exit.failCause(Cause.combine(Cause.fail("rejected"), Cause.die("broken")))))
//   if (Exit.isFailure(failed)) try Console.log(try Effect.try(() => JSON.stringify(failed.cause.reasons.map((reason) => reason._tag))))
12
["Fail","Die"]

Because Exit is a subtype of Effect, try outcome replays its constant result immediately—returning .value or propagating .cause without re-running the original computation. Combining try exit(program) with a later try outcome decouples inspection from propagation, while try onExit(program, (outcome) => cleanup(outcome)) attaches finalizers that observe the program's exit.

Coming from Effect

Exit.succeed(value) builds exit data, try exit(work()) captures a program's exit, and try outcome propagates it.

Replace legacy finally (exit) { ... } clauses with Effect.onExit. Unlike the legacy clause (which passed Exit.void on success), Effect.onExit passes the program's actual Exit.Success value; to match the old contract without discarding the program's return value, replace the exit passed to cleanup rather than wrapping the program in Effect.asVoid:

effect function cleanup(outcome: Exit.Exit<unknown, unknown>) {
  try Console.log(outcome._tag, Exit.isSuccess(outcome) ? outcome.value : "failed")
}
// Run in an effect function: try Console.log("result", try Effect.onExit(Effect.succeed(12), (outcome) => cleanup(outcome)), try Effect.onExit(Effect.succeed(12), (outcome) => cleanup(Exit.isSuccess(outcome) ? Exit.void : outcome)))
Success 12
Success undefined
result 12 12

The first call uses native Effect.onExit; the second converts successful exits to Exit.void for cleanup while preserving 12 as the return value.


The matching Effect page: effect.website/docs/v4/data-types/exit