• Retries a promise-returning function a specified number of times with an optional delay between attempts if it fails.

    Example


    import { retry } from "@mrspartak/promises"
    import { apiCall } from "./api"

    // Retry the API call up to 3 times with a delay of 1000 milliseconds between attempts
    const [error, result] = await retry(() => apiCall(), 3, { delay: 1000 })
    if (error) {
    // error will always be an error returneb by a promise rejection
    }

    Type Parameters

    • T

      The type of the value that the promise resolves to.

    Parameters

    • fn: (() => Promise<T>)

      The promise-returning function to be retried.

        • (): Promise<T>
        • Returns Promise<T>

    • attempts: number

      The number of retry attempts. Must be a positive integer.

    • Optional options: RetryOptions

      Optional settings for retry behavior.

    Returns Promise<RetryOut<T>>

    A promise that resolves to a tuple containing either an error or a value, and the duration in milliseconds.