retry es-toolkit
Retries a Promise-returning function until it succeeds.
Usage
retry(func, options?)
Use retry when you want to automatically retry an asynchronous function that fails. This is useful for operations that can temporarily fail, such as API calls or network requests.
You can configure the number of retries, retry interval, and cancellation signal. The retry interval can be a fixed value or a function that dynamically calculates based on the retry count.
You can use shouldRetry option when you want to retry only on specific errors.
You can also cancel retries using AbortSignal.
Parameters
The asynchronous function to retry.
The number of retries or options object.
retries(number, optional): The number of times to retry. Defaults toInfinityfor infinite retries.delay(number | (attempts: number, error: unknown) => number, optional): The retry interval (in milliseconds). Can be a number or a function. The function receives the attempt number and the error object. Defaults to0.signal(AbortSignal, optional): A signal that can cancel retries.shouldRetry((error: unknown, attempt: number) => boolean, optional): A function that determines whether to retry. If it returnsfalse, the error is thrown immediately.error: The error object that occurred.attempt: The current attempt count (starting from 0).
Returns
Returns the result of the successfully executed function.
Throws
Throws the last error when the retry count is exceeded or when canceled by AbortSignal.
Source: es-toolkit
Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.