Function: waitFor()

waitFor<T>(predicate, options?): Promise<NonNullable<T>>

Polls a predicate until it returns a truthy value, then resolves with that value. This is the right tool when the operation did not fail — the result is simply not ready yet (a DOM element that has not rendered, a flag that has not been set, data that has not arrived).

The predicate is evaluated immediately, then every interval ms. May be sync or async; a rejected predicate is treated as "not ready".

Type Parameters

T

T

Parameters

predicate

() => T | Promise<T>

Evaluated repeatedly until it returns a truthy value.

options?

WaitForOptions

Polling behaviour — see WaitForOptions.

Returns

Promise<NonNullable<T>>

A Promise resolving with the first truthy value.

Example

// Wait for an element to appear (DOM scripting)
const modal = await waitFor(
  () => document.querySelector<HTMLElement>('.modal'),
  { timeout: 10_000 }
)

// Wait for async state
await waitFor(async () => (await fetchStatus()).ready, { interval: 500 })