Zap Studio

Getting Started

Install retry and run your first retry policy.

Installation

npm install @zap-studio/retry
yarn add @zap-studio/retry
pnpm add @zap-studio/retry
bun add @zap-studio/retry
deno add jsr:@zap-studio/retry

Quick Start

Use a built-in policy and run work through policy.run(...).

import { ExponentialBackoff } from "@zap-studio/retry/exponential-backoff";
import { $fetch } from "@zap-studio/fetch";

const policy = new ExponentialBackoff({
  maxAttempts: 5,
  baseDelayMs: 100,
  maxDelayMs: 2_000,
});

const result = await policy.run(async () => {
  const response = await $fetch("https://api.example.com/users", {
    throwOnFetchError: true,
  });
  return await response.json();
});

By default, exhaustion throws RetryError.

Use throwOnExhausted: false when the caller should receive a result object instead:

const result = await policy.run(fetchUsers, {
  throwOnExhausted: false,
});

if (!result.ok) {
  console.error(result.attempts);
  console.error(result.error.lastError);
} else {
  console.log(result.value);
}
Edit on GitHub

Last updated on

On this page