Function retry

  • Continuously retries a given function until it succeeds or the number of retries is exceeded.

    Type Parameters

    • R

    Parameters

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

      The function to retry.

    • retries: number

      The number of times to retry the function.

    • interval: number

      The time to wait between retries. (ms)

    Returns Promise<RetryResult<R>>

    A promise that resolves to a RetryResult.

    Remarks

    The function will be called retries + 1 times. The result can can also be checked with the helper isRetryOK.

    Example

    Basic usage

    // if the fetch fails, retry 3 times with a 1 second interval
    const result = await retry(
    () => fetch('https://example.com'),
    3,
    1000
    )
    if ('ok' in result) {
    console.log(result.ok)
    } else {
    console.error(result.err)
    }

    // or
    import { isRetryOK } from 'uft'

    if (isRetryOK(result)) {
    console.log(result.ok)
    } else {
    console.error(result.err)
    }

Generated using TypeDoc