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

abortable-retry

v1.0.0

Published

Retry async functions with exponential backoff, jitter, per-attempt timeouts, and AbortSignal support. Zero dependencies, fully typed.

Readme

abortable-retry

npm version CI license

Retry async functions with exponential backoff, jitter, per-attempt timeouts, and first-class AbortSignal support. Zero dependencies, fully typed, about 1 kB.

import { retry } from "abortable-retry";

const user = await retry(
  ({ signal }) => fetch(`/api/users/${id}`, { signal }).then((r) => r.json()),
  { retries: 4, delay: 300, timeout: 5_000, signal: controller.signal },
);

Each attempt gets its own signal that aborts when the per-attempt timeout fires or when your outer signal aborts. Pass it to fetch, database drivers, or anything else that accepts one, and in-flight work is cancelled instead of leaking.

Even if the function ignores its signal, retry still settles: an abort rejects the caller right away, and a timeout fails that attempt and moves on. Your code never hangs on a promise that forgot to listen.

Install

npm install abortable-retry

Node 18 or newer. ESM and CommonJS, TypeScript types included.

API

retry(fn, options?)

Runs fn({ attempt, signal }) until it resolves.

| Option | Default | Description | |---------------|-----------------|-------------| | retries | 3 | Retries after the first failure. Total attempts is retries + 1. | | delay | 200 | Base delay in ms, or (attempt, error) => ms for full control. | | backoff | "exponential" | "exponential", "linear", or "fixed". Ignored when delay is a function. | | factor | 2 | Multiplier for exponential backoff. | | maxDelay | 30000 | Cap on any computed delay. | | jitter | "full" | "full" picks in [0, delay], "equal" in [delay/2, delay], false disables. | | signal | | AbortSignal that cancels waits and the in-flight attempt. | | timeout | | Per-attempt timeout in ms. The attempt's signal aborts with a TimeoutError. | | shouldRetry | retry all | (error, attempt) => boolean \| Promise<boolean>. Return false to stop. | | onRetry | | (error, attempt, delayMs) => void, called before each wait. |

Outcomes:

  • Resolves with fn's value on the first success.
  • Rejects with RetryError when every attempt failed. error.errors holds each attempt's error, error.cause is the last one, error.attempts is the count.
  • Rejects with AbortError when options.signal aborts, whether during a wait or mid-attempt.
  • Rejects with the original error, immediately, when fn throws bail(error).

Errors whose name is "AbortError" are never retried.

bail(error)

Throw this from inside fn to give up without retrying. The caller receives error itself.

await retry(async ({ signal }) => {
  const res = await fetch(url, { signal });
  if (res.status === 404) throw bail(new Error("Not found"));
  if (!res.ok) throw new Error(`HTTP ${res.status}`); // retried
  return res.json();
});

backoffDelay(attempt, options?, error?)

The delay calculation on its own, in case you want to schedule something yourself. Takes delay, backoff, factor, maxDelay, and jitter.

backoffDelay(3, { delay: 100, jitter: false }); // 400

sleep(ms, signal?)

Promise-based sleep that rejects with AbortError when the signal aborts.

Error classes

RetryError, AbortError, and TimeoutError are exported so you can check with instanceof.

Recipes

Retry only on network errors and 5xx:

await retry(callApi, {
  shouldRetry: (err) => err instanceof TypeError || (err.status >= 500 && err.status < 600),
});

Honour a Retry-After header:

await retry(callApi, {
  delay: (attempt, err) => err.retryAfterMs ?? 200 * 2 ** (attempt - 1),
});

Cancel from a request handler when the client disconnects:

app.get("/report", async (req, res) => {
  const ac = new AbortController();
  req.on("close", () => ac.abort());
  const data = await retry(({ signal }) => buildReport({ signal }), { signal: ac.signal, retries: 2 });
  res.json(data);
});

Log every retry:

await retry(task, {
  onRetry: (err, attempt, ms) => console.warn(`attempt ${attempt} failed (${err.message}), retrying in ${ms}ms`),
});

License

MIT