npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@jbingen/fetchretry

v0.1.0

Published

Tiny fetch wrapper with retries, exponential backoff, timeout, and abort support.

Readme

🔁 fetchretry

npm version npm bundle size license

Tiny fetch wrapper with retries, exponential backoff, timeout, and abort support.

For anyone tired of writing try/catch/sleep loops around every API call.

npm install @jbingen/fetchretry
// before
let res, attempts = 0;
while (attempts < 3) {
  try { res = await fetch(url); if (res.ok) break; } catch {}
  await new Promise(r => setTimeout(r, 1000 * 2 ** attempts++));
}

// after
const res = await fetchRetry(url);

Returns a standard Response. Works everywhere fetch works.

import { fetchRetry } from "@jbingen/fetchretry";

const res = await fetchRetry("https://api.example.com/data");

const res = await fetchRetry("https://api.example.com/data", {
  retries: 5,
  delay: 500,
  timeout: 10_000,
});

Why

Every production app needs retry logic for HTTP calls. Transient 503s, rate limits, network blips - they all need the same pattern: retry with backoff, respect Retry-After, time out eventually, and let the caller abort.

Everyone writes this. Nobody writes it the same way twice. fetchretry does it in ~90 lines with zero dependencies.

API

fetchRetry(url, init?)

Pre-configured instance using global fetch. Drop-in replacement with retry defaults.

import { fetchRetry } from "@jbingen/fetchretry";

const res = await fetchRetry("https://api.example.com/data");

createFetchRetry(defaults?)

Creates a configured instance. Use this to set base options or provide a custom fetch.

import { createFetchRetry } from "@jbingen/fetchretry";

const apiFetch = createFetchRetry({
  retries: 5,
  delay: 500,
  timeout: 10_000,
});

const res = await apiFetch("https://api.example.com/data");

Per-request options override the defaults:

const res = await apiFetch("https://api.example.com/data", {
  retries: 1,
  timeout: 5_000,
});

Options

| Option | Type | Default | Description | |--------|------|---------|-------------| | retries | number | 3 | Max retry attempts | | delay | number | 1000 | Base delay in ms before first retry | | backoff | number | 2 | Multiplier for exponential backoff | | timeout | number | 30000 | Per-request timeout in ms | | retryOn | (res, attempt) => boolean | status-based | Custom retry predicate | | onRetry | (error, attempt) => void | - | Called before each retry | | signal | AbortSignal | - | External abort signal |

Default retry behavior

Retries on these status codes: 408, 429, 500, 502, 503, 504.

Retries on network errors (fetch throws).

Does not retry on client errors (4xx other than 408/429).

Override with retryOn for custom logic:

const fetch = createFetchRetry({
  retryOn: (res) => res.status >= 500 || res.status === 401,
});

Retry-After

When the server sends a Retry-After header, fetchretry respects it instead of using the calculated backoff delay. Supports both seconds (120) and HTTP-date formats.

Timeout

Each attempt has its own timeout. If a request takes longer than timeout ms, it's aborted and counts as a failed attempt (triggering a retry if attempts remain).

const fetch = createFetchRetry({ timeout: 5_000 });

Abort

Pass an AbortSignal to cancel the entire retry chain. Once aborted, no further retries are attempted.

const controller = new AbortController();
setTimeout(() => controller.abort(), 15_000);

const res = await fetchRetry(url, { signal: controller.signal });

Custom fetch

Provide your own fetch implementation (useful for testing or platforms with non-standard fetch):

const fetch = createFetchRetry({
  fetch: myCustomFetch,
});

Design decisions

  • Zero dependencies. ~90 lines of TypeScript.
  • Returns a standard Response - no custom wrapper, no .json() override, no magic.
  • Exponential backoff by default, Retry-After takes precedence when present.
  • Timeout is per-attempt, not total. Each retry gets a fresh timeout window.
  • External abort signal cancels the entire chain, not just the current attempt.
  • Retryable status codes match common CDN/proxy behavior (408, 429, 5xx).
  • createFetchRetry for configured instances, fetchRetry for quick one-offs.