Hyperlinkv0.8.0-beta.28

Channel

Channel.onExitconsteffect/Channel.ts:6987
<OutDone, OutErr, Env2>(
  finalizer: (
    e: Exit.Exit<OutDone, OutErr>
  ) => Effect.Effect<unknown, never, Env2>
): <OutElem, InElem, InErr, InDone, Env>(
  self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>
) => Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env2 | Env>
<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, Env2>(
  self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  finalizer: (
    e: Exit.Exit<OutDone, OutErr>
  ) => Effect.Effect<unknown, never, Env2>
): Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env2 | Env>

Returns a channel with an exit-aware finalizer that is guaranteed to run once the channel begins execution, whether it succeeds or fails.

Example (Running exit finalizers)

import { Channel, Console, Data, Exit } from "effect"

class ExitError extends Data.TaggedError("ExitError")<{
  readonly stage: string
}> {}

// Create a channel
const dataChannel = Channel.fromIterable([1, 2, 3])

// Attach exit handler
const channelWithExit = Channel.onExit(dataChannel, (exit) => {
  if (Exit.isSuccess(exit)) {
    return Console.log(`Channel completed successfully with: ${exit.value}`)
  } else {
    return Console.log(`Channel failed with: ${exit.cause}`)
  }
})
resource management
Source effect/Channel.ts:698719 lines
export const onExit: {
  <OutDone, OutErr, Env2>(
    finalizer: (e: Exit.Exit<OutDone, OutErr>) => Effect.Effect<unknown, never, Env2>
  ): <OutElem, InElem, InErr, InDone, Env>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>
  ) => Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env2 | Env>
  <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, Env2>(
    self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
    finalizer: (e: Exit.Exit<OutDone, OutErr>) => Effect.Effect<unknown, never, Env2>
  ): Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env2 | Env>
} = dual(2, <OutElem, OutErr, OutDone, InElem, InErr, InDone, Env, Env2>(
  self: Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env>,
  finalizer: (e: Exit.Exit<OutDone, OutErr>) => Effect.Effect<unknown, never, Env2>
): Channel<OutElem, OutErr, OutDone, InElem, InErr, InDone, Env2 | Env> =>
  fromTransformBracket((upstream, scope, forkedScope) =>
    Scope.addFinalizerExit(forkedScope, finalizer as any).pipe(
      Effect.andThen(toTransform(self)(upstream, scope))
    )
  ))
Referenced by 4 symbols