Hyperlinkv0.8.0-beta.28

Scope

Scope.addFinalizerExitconsteffect/Scope.ts:366
(
  scope: Scope,
  finalizer: (exit: Exit<any, any>) => Effect<unknown>
): Effect<void>

Registers an exit-aware finalizer on a scope.

When to use

Use when cleanup needs to know whether the scope closed with success, failure, or interruption.

Details

If the scope is open, the finalizer runs when the scope closes and receives the scope's exit value. If the scope is already closed, the finalizer runs immediately with the stored exit value.

Example (Adding an exit-aware finalizer)

import { Console, Effect, Exit, Scope } from "effect"

const withResource = Effect.gen(function*() {
  const scope = yield* Scope.make()

  // Add a finalizer for cleanup
  yield* Scope.addFinalizerExit(
    scope,
    (exit) =>
      Console.log(
        `Cleaning up resource. Exit: ${
          Exit.isSuccess(exit) ? "Success" : "Failure"
        }`
      )
  )

  // Use the resource
  yield* Console.log("Using resource")

  // Close the scope
  yield* Scope.close(scope, Exit.void)
})
combinators
Source effect/Scope.ts:3662 lines
export const addFinalizerExit: (scope: Scope, finalizer: (exit: Exit<any, any>) => Effect<unknown>) => Effect<void> =
  effect.scopeAddFinalizerExit
Referenced by 5 symbols