@bnidev/js-utils
    Preparing search index...

    Function retryAsyncFn

    • Retries a promise-returning function a specified number of times with delay between attempts.

      Type Parameters

      • T

        The return type of the async function.

      Parameters

      • fn: () => Promise<T>

        The async function to retry.

      • retries: number = 3

        Maximum number of retry attempts. Default is 3.

      • delay: number = 500

        Delay between retries in milliseconds. Default is 500ms.

      Returns Promise<T>

      A promise that resolves with the result of the function, or rejects after all retries fail.

      This utility is useful for handling transient errors in asynchronous operations, such as network requests or database queries, where a retry might succeed after a failure.

      // ES Module
      import { retryAsyncFn } from '@bnidev/js-utils'

      // CommonJS
      const { retryAsyncFn } = require('@bnidev/js-utils')
      await retryAsyncFn(() => fetch('/api/data'), 5, 1000);