21 lines
582 B
TypeScript
Raw Permalink Normal View History

export function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export async function eventually(assertFn: () => void, opts?: { timeoutMs?: number; intervalMs?: number }): Promise<void> {
const timeoutMs = opts?.timeoutMs ?? 500;
const intervalMs = opts?.intervalMs ?? 10;
const start = Date.now();
let lastErr: any = null;
while (Date.now() - start < timeoutMs) {
try {
assertFn();
return;
} catch (e: any) {
lastErr = e;
await sleep(intervalMs);
}
}
if (lastErr) throw lastErr;
}