@pathmaker-digital/pmd-utilities - v2.2.6
    Preparing search index...

    Function repeatUntil

    • Repeatedly executes a function until a specified condition is met or a maximum number of attempts is reached. Useful for retrying an operation with an interval or condition check.

      Type Parameters

      • T

      Parameters

      • fn: () => T

        The function to execute repeatedly.

      • predicate: (val: T) => boolean

        A function that checks if the result of fn meets the desired condition.

      • maxTries: number = 10

        Maximum number of attempts to execute the function (default is 10).

      Returns T

      The result of fn once the predicate is satisfied or the maximum attempts are reached.

      // Attempts to generate a valid email address, retrying up to 5 times
      const result = repeatUntil(generateEmail, email => email.includes('@'), 5);
      // Retries generating a random number between 1 and 5 until it's greater than 3
      const number = repeatUntil(() => Math.floor(Math.random() * 5) + 1, n => n > 3);
      console.log(number); // Outputs a number greater than 3