Skip to main content

Signature

  • run: Executes a single task
  • all: Executes multiple tasks with concurrency control
  • runOrThrow: Executes a single task and throws the result error if it fails
  • allOrThrow: Executes multiple tasks and throws the first failure
  • partitionAll: Splits results from all() into successes and failures
  • withConfig: Creates a new instance with merged configuration
  • orThrow: Alias for runOrThrow

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 tryo, { errorRule } from 'tryo'

const runner = tryo({
  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')