UNPKG

redis-smq-common

Version:

RedisSMQ Common Library provides many components that are mainly used by RedisSMQ and RedisSMQ Monitor.

31 lines 1.09 kB
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