/**
 * Poll an async function until it returns a non-null/non-undefined value, or
 * until the timeout is reached.
 *
 * @param fn        async function to call on each attempt; return null/undefined to keep polling
 * @param options   timing options
 * @returns         the first non-null result, or null if the timeout was reached
 */
export async function pollUntil(fn, options = {}) {
    const { intervalMs = 2000, timeoutMs = 30_000, initialDelayMs = 0 } = options;
    if (initialDelayMs > 0) {
        await sleep(initialDelayMs);
    }
    const deadline = Date.now() + timeoutMs;
    let attempt = 0;
    while (Date.now() < deadline) {
        if (attempt > 0) {
            await sleep(intervalMs);
        }
        const result = await fn(attempt);
        if (result != null) {
            return result;
        }
        attempt++;
    }
    return null;
}
export function sleep(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms));
}
//# sourceMappingURL=async-poll.js.map