wundertec-core
Version:
Librería estándar de utilidades e integraciones AWS + helpers generales
30 lines (29 loc) • 1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.retry = retry;
const backoff_1 = require("./backoff");
const sleep_1 = require("../utils/sleep");
/**
* Reintenta una función asíncrona en caso de fallo, usando backoff exponencial.
* @param fn Función que retorna una promesa.
* @param options Configuración de reintentos.
* @returns Resultado de la función si tiene éxito.
* @throws Último error si agota los intentos.
*/
async function retry(fn, options = {}) {
const { retries = 3, baseDelay = 100, maxDelay = 1000 } = options;
let lastError;
for (let attempt = 0; attempt <= retries; attempt++) {
try {
return await fn();
}
catch (err) {
lastError = err;
if (attempt < retries) {
const delay = (0, backoff_1.exponentialBackoff)(attempt, baseDelay, maxDelay);
await (0, sleep_1.sleep)(delay);
}
}
}
throw lastError;
}