Hyperlinkv0.8.0-beta.28

Effect

Effect.runCallbackWithconsteffect/Effect.ts:8932
<R>(context: Context.Context<R>): <A, E>(
  effect: Effect<A, E, R>,
  options?:
    | (RunOptions & { readonly onExit: (exit: Exit.Exit<A, E>) => void })
    | undefined
) => (interruptor?: number | undefined) => void

Forks an effect with the provided services, registers onExit as a fiber observer, and returns an interruptor.

When to use

Use when embedding an effect into callback-style code with explicit services and a synchronous interruptor.

Details

The returned interruptor calls fiber.interruptUnsafe, optionally with an interruptor id.

Example (Running with services and a callback)

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

interface Logger {
  log: (message: string) => Effect.Effect<void>
}

const Logger = Context.Service<Logger>("Logger")

const services = Context.make(Logger, {
  log: (message) => Console.log(message)
})

const program = Effect.gen(function*() {
  const logger = yield* Logger
  yield* logger.log("Started")
  return "done"
})

const interrupt = Effect.runCallbackWith(services)(program, {
  onExit: (exit) => {
    if (Exit.isFailure(exit)) {
      // handle failure or interruption
    }
  }
})

// Use the interruptor if you need to cancel the fiber later.
interrupt()
running
Source effect/Effect.ts:89326 lines
export const runCallbackWith: <R>(
  context: Context.Context<R>
) => <A, E>(
  effect: Effect<A, E, R>,
  options?: (RunOptions & { readonly onExit: (exit: Exit.Exit<A, E>) => void }) | undefined
) => (interruptor?: number | undefined) => void = internal.runCallbackWith
Referenced by 1 symbols