Skip to content

waitUntil

waitUntil<T>(condition, options): Promise<T>

Repeatedly evaluates a synchronous or asynchronous condition function until it returns a truthy value, using exponential backoff between retries.

Supports an overall timeout and optional AbortSignal for cancellation.

T

Return value

() => T | Promise<T>

A function that returns a truthy value or a Promise resolving to truthy. waitUntil resolves with that truthy value.

WaitUntilOptions = {}

Settings for how long and how often to retry.

Promise<T>

Resolves with the first truthy result from condition.

If the provided signal is aborted, or the timeout elapses.

// Wait for a DOM element to appear (up to 5 seconds)
const btn = await waitUntil(
() => document.querySelector<HTMLButtonElement>('#submit'),
{ timeout: 5000 }
)
btn.click()
// Poll an API until it reports readiness
const readyData = await waitUntil(
async () => {
const resp = await fetch('/api/status')
const json = await resp.json()
return json.ready ? json : null
},
{ initialInterval: 100, maxInterval: 2000, timeout: 15000 }
)
console.log('Service is ready:', readyData)