@consumidor-positivo/ts-utils - v1.13.0
    Preparing search index...

    Function untilLoad

    • Repeatedly executes an action until it returns a truthy value or a timeout is reached.

      This utility function is useful for polling operations, such as waiting for an element to appear in the DOM, or waiting for a resource to load.

      Type Parameters

      • T

      Parameters

      • action: () => T

        The function to execute repeatedly until it returns a truthy value

      • OptionaltimeoutTime: number

        Optional timeout in milliseconds after which the function will return whatever value it has

      • intervalTime: number = 100

        Time in milliseconds between execution attempts (default: 100)

      Returns Promise<T>

      A promise that resolves with the truthy value returned by the action, or the last value if a timeout occurs

      import { untilLoad } from '@consumidor-positivo/ts-utils';

      // Wait until an element exists in the DOM
      const element = await untilLoad(() => document.querySelector('#my-element'));

      // Wait until a condition is met, with a 5 second timeout
      const result = await untilLoad(() => {
      const data = getDataFromSomewhere();
      return data?.isReady ? data : null;
      }, 5000);