mini-effect
    Preparing search index...

    Function gen

    • Creates an Effect from a generator function, enabling imperative-style yield* syntax for composing effects.

      Type Parameters

      • T extends Effect<any, any, any, any>

        The Effect type(s) yielded within the generator

      • TReturn

        The return type of the generator (the effect's success value)

      • TNext

        The type passed back into the generator on each yield* resumption

      Parameters

      • thunk: (
            signal: AbortSignal,
            ctx: Record<string, unknown>,
        ) => Generator<T, TReturn, TNext>

        A generator function that yields effects and returns a final value. It receives an AbortSignal and an internal context record.

      • OptionallocalContext: boolean

        When true, the generator operates on a shallow copy of the dependency context, isolating mutations from the outer scope

      Returns Effect<
          TReturn,
          inferError<T>,
          Exclude<inferDependency<T>, inferProvides<T>>,
          inferProvides<T>,
      >

      An Effect whose success value is the generator's return value, with error and dependency types inferred from the yielded effects

      Inside the generator, use yield* to unwrap other effects and obtain their success values. The generator receives an AbortSignal for cancellation awareness and a context record for internal dependency propagation.

      Errors thrown by any yielded effect will propagate as the error type of the resulting effect. Dependencies required by yielded effects are automatically tracked and surfaced in the return type (minus any that are provided internally).

      import { gen, fn, succeed, run } from "mini-effect";

      const program = gen(function* (signal) {
      const a = yield* fn(() => Promise.resolve(1));
      const b = yield* succeed(2);
      return a + b;
      });

      await run(program); // => 3