export type WaitOptions = { description: string; timeoutMs?: number; intervalMs?: number; }; export type Truthy = Exclude; /** Polls `probe` until it returns a truthy value, then returns that value. */ export async function waitUntil( probe: () => T | Promise, { description, timeoutMs = 15_000, intervalMs = 100 }: WaitOptions, ): Promise> { const deadline = Date.now() + timeoutMs; for (;;) { const value = await probe(); if (value) return value as Truthy; if (Date.now() >= deadline) { throw new Error(`Timed out after ${timeoutMs}ms waiting for ${description}`); } await new Promise((resolve) => setTimeout(resolve, intervalMs)); } }