Tweakr
    Preparing search index...

    Function retryUntil

    • Retries an asynchronous function until a given condition returns true or the maximum number of retries is reached.

      Each attempt waits for the previous promise to resolve before checking the condition. A delay is applied between retries.

      Type Parameters

      • T

        The type of the value returned by the async function.

      Parameters

      • fn: () => Promise<T>

        The asynchronous function to execute.

      • condition: (result: T) => boolean

        A predicate function that checks if the result is acceptable.

      • retries: number = 5

        Maximum number of attempts. Defaults to 5.

      • delay: number = 100

        Delay in milliseconds between retries. Defaults to 100.

      Returns Promise<T>

      A promise that resolves to the first result satisfying the condition, or the last result if none satisfy.

      let counter = 0;
      const result = await retryUntil(
      async () => {
      counter++;
      return counter;
      },
      (val) => val >= 3,
      5,
      100
      );

      console.log(result); // 3

      1.1.0