mirror of
https://github.com/MrUnknownDE/VRCX.git
synced 2026-04-10 10:23:52 +02:00
25 lines
676 B
JavaScript
25 lines
676 B
JavaScript
export async function executeWithBackoff(fn, options = {}) {
|
|
const {
|
|
maxRetries = 5,
|
|
baseDelay = 1000,
|
|
shouldRetry = () => true
|
|
} = options;
|
|
|
|
async function attempt(remaining) {
|
|
try {
|
|
return await fn();
|
|
} catch (err) {
|
|
if (remaining <= 0 || !shouldRetry(err)) {
|
|
throw err;
|
|
}
|
|
const delay =
|
|
baseDelay *
|
|
Math.pow(2, (options.maxRetries || maxRetries) - remaining);
|
|
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
return attempt(remaining - 1);
|
|
}
|
|
}
|
|
|
|
return attempt(maxRetries);
|
|
}
|