API Reference

p-retry

Retry helper for promise-returning functions with exponential backoff, attempt hooks, and hard cancelation signals.

Official Documentation ↗
CompatibilityThis documentation covers version v9.0.0. Check the official docs for the latest API surface.

Installation

npmnpm install p-retry
pnpmpnpm add p-retry

Methods

Retry helper for promise-returning functions with exponential backoff, attempt hooks, and hard cancelation signals.

Overview

p-retry retries a promise-returning function with the exponential-backoff schedule from the retry module: delays grow by factor from minTimeout up to maxTimeout. The function receives the attempt number, which lets you rotate endpoints or refresh tokens between tries.

Retries are a policy, and p-retry exposes the knobs that policy needs: onFailedAttempt for observability, AbortError for definitive failures, and shouldRetry to classify which errors deserve another round. Everything else is deliberately left to the caller, including deadline budgets around the loop.

Core Methods

The most frequently used entry points, with signatures and runnable examples:

  • pRetry — Retries the function on failure with configurable retries, factor, and minTimeout that shape exponential delays.
  • AbortError — Throwing this error stops further retries immediately and rejects with the wrapped reason.
  • onFailedAttempt — Callback after each failed try, exposing attemptNumber, retriesLeft, and the error for logging or circuit decisions.

Usage Notes

  • Set maxTimeout so a large factor cannot produce multi-minute waits that outlive your request budget.
  • Log every attempt with onFailedAttempt; silent retries turn transient incidents into invisible latency.
  • Cap total wall time with your own deadline: retries times worst delay often exceeds the caller tolerance of the operation.

Common Pitfalls

  • Retrying non-idempotent POST calls can duplicate payments; restrict retries to safe operations unless the endpoint is idempotency-keyed.
  • Retries reset nothing downstream: a wedged connection pool will fail every attempt at the same speed.
  • The default retries value is 10, which is usually too many for user-facing requests.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/sindresorhus/p-retry@latest/readme.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:51:31.902Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

FAQ

Which errors should not be retried?
Do not retry definitive failures: 400-level responses, validation errors, or a resource that is known to be gone. Throw AbortError for those so the caller gets the real error instead of pointless extra attempts.
How do I add jitter to the backoff?
Replace the wait with a custom randomize strategy: set randomize true for built-in jitter, or pass a calculateDelay function that mixes Math.random into the computed delay when many clients retry in lockstep.
Does p-retry cancel the underlying work?
No. It only declines to call the function again. To stop in-flight work, pass an AbortSignal into your fetch call and abort it from a timeout wrapper outside p-retry.