Skip to main content

Import

What it does

RetryPolicy wraps any async function with automatic retries using exponential backoff and jitter. When all attempts are exhausted, the last error is thrown. A configurable shouldRetry predicate controls which errors trigger retries.
ApiClient creates an internal RetryPolicy from the retryPolicy config option. Use this class directly when you need retries outside of HTTP requests.

Constructor

RetryConfig

The built-in shouldRetry predicate retries on:
  • HTTP status 408 (timeout), 429 (rate limit), and 5xx (server errors)
  • Network-related error messages (timeout, network, ECONNREFUSED, ECONNRESET)

Methods

execute<T>(fn)

Runs fn up to maxAttempts times. Returns the result on the first success. Throws the last error if all attempts fail.

getConfig()

Returns the resolved configuration object, useful for debugging or logging.

How backoff works

The delay between retries follows this formula: delay = min( initialDelayMs × backoffMultiplier^(attempt - 1) + jitter, maxDelayMs ) Jitter is added automatically (up to 10% of the exponential delay) to prevent thundering-herd effects when many clients retry at the same time.

Examples

Basic retry

Custom shouldRetry predicate

Wrapping a non-HTTP operation

RetryPolicy does not deduplicate in-flight calls. If you call execute concurrently with the same function, each call retries independently. Combine with RequestDeduplicator if you need deduplication.