/** * Resolves or rejects with the provided promise, but fails if it does not settle within `timeoutMs`. */ export const withTimeout = (promise: Promise, timeoutMs: number): Promise => { let timer: NodeJS.Timeout | null = null; const timeout = new Promise((_, reject) => { timer = setTimeout(() => reject(new Error('Operation timed out')), timeoutMs); }); return Promise.race([promise, timeout]).finally(() => { if (timer) { clearTimeout(timer); } }); };