/**
 * Polls a function until it returns a result that satisfies the predicate.
 */
export async function poll(input) {
    const start = Date.now();
    let delay = input.initialDelay ?? 250;
    while (true) {
        const result = await input.fn();
        if (input.predicate(result)) {
            return result;
        }
        if (Date.now() - start >= (input.timeout ?? 1_000_000)) {
            throw new Error(`Timed out waiting for ${input.description} after ${Math.round((input.timeout ?? 1_000_000) / 1000)}s`);
        }
        await new Promise((resolve) => setTimeout(resolve, delay));
        delay = Math.min(delay * 2, input.maxDelay ?? 5_000);
    }
}
//# sourceMappingURL=poll.js.map