The function to execute repeatedly until it returns a truthy value
Optional
timeoutTime: numberOptional timeout in milliseconds after which the function will return whatever value it has
Time in milliseconds between execution attempts (default: 100)
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);
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.