Skip to main content

Main options

  • retries: number of retries
  • retryDelay: number, function, or based on attempt and last error
  • jitter: randomness to prevent thundering herd
  • backoffStrategy: linear, exponential, fibonacci or function
  • maxDelay: upper limit of delay
  • shouldRetry: decides whether to continue retrying
  • onRetry: observability per attempt

Basic example

import { run } from "trybox";

const r = await run(() => fetch("/api"), {
  retries: 3,
  retryDelay: 300,
  jitter: 0.5,
  backoffStrategy: "exponential",
});

Dynamic delay

const r = await run(() => fetch("/api"), {
  retries: 5,
  retryDelay: (attempt) => attempt * 200,
  maxDelay: 2000,
});

Retry control with shouldRetry

const r = await run(() => fetch("/api"), {
  retries: 5,
  shouldRetry: (attempt, error, ctx) => error.code !== "ABORTED" && ctx.elapsedTime < 5000,
});

Observability with onRetry

const r = await run(() => fetch("/api"), {
  retries: 3,
  onRetry: (attempt, error, nextDelay) => {
    console.log(attempt, error.code, nextDelay);
  },
});