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

@bredele/retry

v1.0.1

Published

Elegant Retry mechanism with max attempts, intervals and backoff rate.

Downloads

12

Readme

retry

Elegant Retry mechanism with max attempts, intervals and exponential backoff rate. Wrap any async method (or sync method) and get an async callback that automatically retries on failure with configurable retry logic.

Installation

npm install @bredele/retry

Usage

import retry from "@bredele/retry";

// Wrap an async function - retries 3 times with 1s intervals and 2x backoff
const retriableFunction = retry(asyncMethod);
await retriableFunction();

// Pass arguments through to the wrapped function
const retriableWithArgs = retry(async (id: string, options: object) => {
  return await api.getData(id, options);
});
await retriableWithArgs("user123", { timeout: 5000 });

API

retry(fn, options?): AsyncFunction

  • fn - The function to wrap (async or sync)
  • options - Optional configuration object
    • errors?: string[] List of error names to retry. If not specified, retries all errors
    • intervals?: number Base retry interval in milliseconds
    • backoff?: number Exponential backoff multiplier
    • maxAttempts?: number Maximum number of attempts (including the initial attempt)
    • maxInterval?: number Maximum delay between retries (prevents infinite growth)
    • jitter?: boolean Add ±20% randomness to delays (prevents thundering herd)

Notes

Error Filtering

When errors is specified, only errors with matching names will trigger retries:

// Only retry specific error types
const retriableFunction = retry(asyncMethod, {
  errors: ["NetworkError", "TimeoutError"],
});

// This will retry on NetworkError but not on ValidationError

Exponential Backoff

The retry intervals follow an exponential backoff pattern:

  • 1st retry: intervals ms
  • 2nd retry: intervals * backoff ms
  • 3rd retry: intervals * backoff² ms
  • And so on...
// With intervals: 1000, backoff: 2
// Retry delays: 1000ms, 2000ms, 4000ms, 8000ms...
const retriableFunction = retry(asyncMethod, {
  intervals: 1000,
  backoff: 2,
  maxAttempts: 5,
});

Enhanced Backoff Features

Maximum Interval Cap

Prevent delays from growing too large:

// Delays: 1000ms, 2000ms, 4000ms, 5000ms, 5000ms...
const retriableFunction = retry(asyncMethod, {
  intervals: 1000,
  backoff: 2,
  maxInterval: 5000, // Cap at 5 seconds
  maxAttempts: 6,
});

Jitter (Anti-Thundering Herd)

Add randomness to prevent all clients from retrying simultaneously:

// Delays: ~1000ms, ~2000ms, ~4000ms (with ±20% variation)
const retriableFunction = retry(asyncMethod, {
  intervals: 1000,
  backoff: 2,
  jitter: true, // Adds 20% randomness
  maxAttempts: 4,
});

Why use jitter? When many clients fail simultaneously (e.g., service outage), they all retry at the same intervals. This can overwhelm the recovering service. Jitter spreads out the retry attempts over time.

Combining Features

// Production-ready configuration
const robustRetry = retry(apiCall, {
  intervals: 1000,
  backoff: 2,
  maxAttempts: 5,
  maxInterval: 30000, // Never wait more than 30 seconds
  jitter: true, // Spread out retries
  errors: ["NetworkError", "TimeoutError"],
});