import { TimeoutError } from './error/TimeoutError.js'; import { PredicateC0 } from './Predicate.js'; /** * Resolve after a set amount of time. * * @public */ export function defer(ms: number): Promise { return new Promise((res, _rej) => { setTimeout(() => { res(); }, ms); }); } /** * Resolve with the given value, after a set amount of time. * * @public */ export function deferValue(ms: number, val: T): Promise { return new Promise((res, _rej) => { setTimeout(() => { res(val); }, ms); }); } /** * Reject after a set amount of time if the original promise has not yet resolved. * * @public */ export function timeout(ms: number, inner: Promise): Promise { const limit = new Promise((_res, rej) => { setTimeout(() => { rej(new TimeoutError()); }, ms); }); return Promise.race([limit, inner]); } /** * Resolve if `cb` returns true within `max` tries, otherwise reject with a `TimeoutError`. * * @public * @throws TimeoutError */ export async function deferUntil(cb: PredicateC0, step: number, max: number): Promise { let count = 0; while (count < max) { await defer(step); if (cb()) { return; } count += 1; } throw new TimeoutError(); } /** * @public * @deprecated */ export async function waitFor(cb: PredicateC0, step: number, tries: number): Promise { return deferUntil(cb, step, tries); }