redis-smq-common
Version:
Provides essential components and utilities shared across RedisSMQ packages.
31 lines • 1.09 kB
JavaScript
export function withRetry(operation, options = {}, callback) {
const maxAttempts = options.maxAttempts ?? 3;
const retryDelay = options.retryDelay ?? 1000;
const shouldRetry = options.shouldRetry ?? (() => 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