mini-effect
    Preparing search index...

    Function fork

    • Runs an Effect in the background and returns a function to abort it.

      Type Parameters

      • const E extends Effect<any, any, any, any>

        The Effect type being forked

      Parameters

      • effect: E

        The effect to execute in the background

      Returns Effect<() => void, never, never, never>

      An Effect that, when run, starts effect in the background and resolves with an abort function () => void to cancel it

      fork starts the given effect immediately in a separate execution context with its own AbortController. The forked effect shares the parent's dependency context but can be cancelled independently by calling the returned abort function.

      If the parent signal is aborted, the forked effect is also aborted automatically. Errors thrown by the forked effect are silently swallowed (.catch() is called on the internal promise).

      import { fn, run } from "mini-effect";
      import { fork } from "mini-effect/concurrency";

      const backgroundTask = fn(() => longRunningWork());
      const abort = await run(fork(backgroundTask));

      // Later, cancel the background work:
      abort();