redis-smq-common
Version:
Provides essential components and utilities shared across RedisSMQ packages.
35 lines • 1.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.withRetry = withRetry;
function withRetry(operation, options = {}, callback) {
var _a, _b, _c;
const maxAttempts = (_a = options.maxAttempts) !== null && _a !== void 0 ? _a : 3;
const retryDelay = (_b = options.retryDelay) !== null && _b !== void 0 ? _b : 1000;
const shouldRetry = (_c = options.shouldRetry) !== null && _c !== void 0 ? _c : (() => true);
let attempts = 0;
const attemptOperation = () => {
attempts++;
try {
operation((err, result) => {
if (err) {
if (attempts < maxAttempts && shouldRetry(err)) {
setTimeout(attemptOperation, retryDelay);
return;
}
return callback(err);
}
return callback(null, result);
});
}
catch (error) {
const err = error instanceof Error ? error : new Error(String(error));
if (attempts < maxAttempts && shouldRetry(err)) {
setTimeout(attemptOperation, retryDelay);
return;
}
return callback(err);
}
};
attemptOperation();
}
//# sourceMappingURL=with-retry.js.map