UNPKG

@lesnoypudge/utils

Version:

lesnoypudge's utils

25 lines (24 loc) 684 B
import { catchErrorAsync } from "../catchErrorAsync/catchErrorAsync.js"; import { sleep } from "../sleep/sleep.js"; const defaultBackoff = (attempt) => { return 100 * Math.min(200, Math.max(0, attempt - 1) ** 3); }; const promiseRetry = async (fn, maxAttempts, backoffFn = defaultBackoff) => { let currentAttempt = 0; while (true) { currentAttempt++; const [value, error] = await catchErrorAsync(fn); if (!error) return value; if (currentAttempt >= maxAttempts) { throw error; } const delay = backoffFn(currentAttempt); if (delay > 0) { await sleep(delay); } } }; export { promiseRetry }; //# sourceMappingURL=promiseRetry.js.map