Skip to main content

Signature

Creates a Runner instance with methods:
  • run: executes a task with a safe result
  • allSettled: executes tasks with concurrency control
  • all: executes tasks and throws on failure

Options

  • rules: list of rules to normalize errors
  • fallback: function for unmapped errors
  • toError: custom complete normalizer
  • ignoreAbort: default for aborts
  • mapError: default error transformation
  • circuitBreaker: default circuit breaker configuration

Example

import trybox, { errorRule } from "trybox";

const runner = trybox({
  ignoreAbort: true,
  circuitBreaker: { failureThreshold: 3, resetTimeout: 2000, halfOpenRequests: 1 },
  rules: [
    errorRule.when((e): e is { code: string } => typeof (e as any)?.code === "string").toError((e) => ({
      code: (e as any).code,
      message: "Mapped error",
    })),
  ],
});

const res = await runner.run(async () => "ok");