trade360-nodejs-sdk
Version:
LSports Trade360 SDK for Node.js
48 lines • 1.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.withRetry = withRetry;
const errors_1 = require("../entities/errors");
/**
* Retry an operation with exponential backoff based
* on the number of attempts and delay.
* @param operation the operation to retry
* @param options the retry options based on the number
* of attempts and delay in milliseconds with an optional.
* backoff factor for exponential backoff (default is 1).
* @param operationName the name of the operation for
* logging purposes.
* @param logger the logger to use for logging the
* operation.
* @returns the result of the operation if it succeeds
* after the number of attempts.
* @throws a RetryError if the operation fails after the
* number of attempts. The error contains the number of
* attempts made.
*/
async function withRetry(operation, options, operationName, logger) {
const { maxAttempts, delayMs, backoffFactor = 1 } = options;
return new Promise((resolve, reject) => {
let attempts = 0;
const executeRetry = async () => {
attempts++;
try {
const result = await operation();
logger.debug(`${operationName} succeeded on attempt ${attempts}`);
resolve(result);
}
catch (error) {
logger.warn(`Attempt ${attempts} failed: ${error}`);
if (attempts >= maxAttempts) {
reject(new errors_1.RetryError(operationName, maxAttempts));
}
else {
const nextDelay = delayMs * Math.pow(backoffFactor, attempts - 1);
logger.debug(`Retrying in ${nextDelay}ms...`);
setTimeout(executeRetry, nextDelay);
}
}
};
executeRetry();
});
}
//# sourceMappingURL=retry-util.js.map