41 lines
1.1 KiB
JavaScript
41 lines
1.1 KiB
JavaScript
'use strict';
|
|
|
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
|
|
const delay = require('../promise/delay.js');
|
|
|
|
const DEFAULT_DELAY = 0;
|
|
const DEFAULT_RETRIES = Number.POSITIVE_INFINITY;
|
|
async function retry(func, _options) {
|
|
let delay$1;
|
|
let retries;
|
|
let signal;
|
|
if (typeof _options === 'number') {
|
|
delay$1 = DEFAULT_DELAY;
|
|
retries = _options;
|
|
signal = undefined;
|
|
}
|
|
else {
|
|
delay$1 = _options?.delay ?? DEFAULT_DELAY;
|
|
retries = _options?.retries ?? DEFAULT_RETRIES;
|
|
signal = _options?.signal;
|
|
}
|
|
let error;
|
|
for (let attempts = 0; attempts < retries; attempts++) {
|
|
if (signal?.aborted) {
|
|
throw error ?? new Error(`The retry operation was aborted due to an abort signal.`);
|
|
}
|
|
try {
|
|
return await func();
|
|
}
|
|
catch (err) {
|
|
error = err;
|
|
const currentDelay = typeof delay$1 === 'function' ? delay$1(attempts) : delay$1;
|
|
await delay.delay(currentDelay);
|
|
}
|
|
}
|
|
throw error;
|
|
}
|
|
|
|
exports.retry = retry;
|