Sign up (with export icon)

utils/retry

Api-module iconmodule

Functions

  • Chevron-right icon

    exponentialDelay( options = { options.delay?, options.factor?, options.maxDelay? } ) → ( attempt: number ) => number

    Creates a function that calculates exponential back-off delay. Pass it as options.retryDelay to retry.

    Parameters

    options: object
    Properties
    options.delay?: number

    Base delay between invocations. Defaults to 1s.

    options.factor?: number

    How much to increase the delay. Defaults to 2x.

    options.maxDelay?: number

    Maximum timeout. Even if higher timeout is calculated, it cannot get higher than this value. Default to 10s.

    Defaults to {}

    Returns

    ( attempt: number ) => number

    The function calculating the delay.

  • Chevron-right icon

    retry( callback, options = { options.maxAttempts?, options.retryDelay?, options.signal? } ) → Promise<TResult>

    Tries calling the given callback until it sucessfully resolves.

    If the callback fails maxAttempts times, the returned promise is rejected with the last error.

    Type parameters

    TResult

    The result of a successful callback invocation.

    Parameters

    callback: () => Promise<TResult>

    The function to call until it succeeds.

    options: object

    Configuration options.

    Properties
    options.maxAttempts?: number

    Maximum number of attempts.

    options.retryDelay?: ( attempt: number ) => number

    The time in milliseconds between attempts. By default it implements exponential back-off policy.

    options.signal?: AbortSignal

    The signal to abort further retries. The callback itself is not aborted automatically.

    Defaults to {}

    Returns

    Promise<TResult>