export default async function poll( fn: () => Promise, wait: number | ((result: T, attempts: number) => number) = 0, until?: Date | ((result: T, attempts: number) => boolean) ): Promise { let attempts = 0; return new Promise((resolve, reject) => { const check = async () => { try { attempts++; const result = await fn(); if (until && (until instanceof Date ? new Date() >= until : until(result, attempts))) { resolve(result); return; } setTimeout(() => void check(), typeof wait === 'function' ? wait(result, attempts) : wait); } catch (err: unknown) { reject(err); } }; void check(); }); }