export const withTimeout = async (durationMs: number, promise: Promise): Promise => { let timer = null; const timeout = new Promise((resolve, reject) => { const rejectCallback = () => { timer = null; reject(new Error('Task timed out')); }; timer = setTimeout(rejectCallback, durationMs); }); const result = await Promise.race([promise, timeout]); if (timer != null) { clearTimeout(timer); } return result; }; export const retry = async (totalAttempts: number, task: () => Promise): Promise => { let attemptsRemaining = totalAttempts; let caughtError = null; while (attemptsRemaining > 0) { try { return await task(); } catch (err) { caughtError = err; attemptsRemaining -= 1; } } throw caughtError; };