p-retry
Retry a promise-returning function with configurable attempts, delays, and exponential backoff.
p-retry
General JSRetry a promise-returning function with configurable attempts, delays, and exponential backoff.
Fit
Bundle (gzip)
1.8 kB
4.3 kB raw
Install size includes transitive dependencies. Bundle size is gzipped browser payload.
Freshness
The Problem
Network requests, database connections, and external API calls fail transiently. Manually wrapping every async call in a retry loop with exponential backoff and error classification is repetitive boilerplate.
What It Does
pRetry wraps any promise-returning function and retries it on failure up to a configurable number of times. It uses got's retry strategy: exponential backoff with jitter by default. You can throw an AbortError to stop retrying when an error is permanent (e.g. 404 Not Found shouldn't be retried).
Installation
npm install p-retryUsage Example
import pRetry, { AbortError } from 'p-retry';
async function fetchUser(id: string) {
const res = await fetch(`/api/users/${id}`);
if (res.status === 404) throw new AbortError('User not found'); // don't retry
if (!res.ok) throw new Error(`HTTP ${res.status}`); // will retry
return res.json();
}
const user = await pRetry(() => fetchUser('abc123'), {
retries: 3,
onFailedAttempt: (error) => {
console.log(`Attempt ${error.attemptNumber} failed. ${error.retriesLeft} retries left.`);
},
});Related packages
Unified TypeScript SDK for building AI-powered apps — switch between 20+ LLM providers without rewriting code.
animejsGeneral JSLightweight JavaScript animation library with a tiny footprint and a powerful, expressive API.
AOSGeneral JSAn easy-to-use library that makes elements fade, slide, or zoom into view as the user scrolls down your page.
archiverGeneral JSStreaming interface for generating ZIP and TAR archives in Node.js — create compressed archives programmatically from files, directories, and buffers.
@formkit/auto-animateGeneral JS> Add smooth transitions to any element with a single line — zero-config animations for adds, removes, and moves.