redo.js
Version:
A simple but powerful library for your retrying operations.
83 lines • 4.13 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.retryAsyncOperation = exports.retryOperation = void 0;
// Utility function to introduce a delay
const sleep = (delay) => {
return new Promise((resolve) => setTimeout(resolve, delay));
};
// Synchronous retry operation with error handling and exponential delay
function retryOperation(_a) {
return __awaiter(this, arguments, void 0, function* ({ retryCallback, onErrorCallback, onSuccessCallback, afterLastAttemptErrorCallback, retryCount = 3, // Default retry count is 3
retryDelay = 1000, // Default is 1 second
incrementalDelayFactor = 1.5, // Default factor is 1.5
}) {
let currentRetryCount = 0;
let lastError = null;
let currentDelay = retryDelay;
// Loop until retries are exhausted or successful
while (retryCount === "infinite" || currentRetryCount <= retryCount) {
try {
if (currentDelay > 0 && currentRetryCount > 0) {
yield sleep(currentDelay); // Wait for the delay if it's not the first attempt
}
const response = retryCallback();
onSuccessCallback(response); // Call success callback on success
return;
}
catch (error) {
lastError = error;
onErrorCallback(error, currentRetryCount); // Handle error with retry count
currentDelay *= incrementalDelayFactor; // Increase the delay for the next retry
currentRetryCount++;
}
}
// Call the final error callback if retries are exhausted
if (afterLastAttemptErrorCallback) {
afterLastAttemptErrorCallback(lastError);
}
});
}
exports.retryOperation = retryOperation;
// Asynchronous retry operation with error handling and exponential delay
function retryAsyncOperation(_a) {
return __awaiter(this, arguments, void 0, function* ({ retryAsyncCallback, onErrorCallback, onSuccessCallback, afterLastAttemptErrorCallback, retryCount = 3, // Default retry count is 3
retryDelay = 1000, // Default is 1 second
incrementalDelayFactor = 1.5, // Default factor is 1.5
}) {
let currentRetryCount = 0;
let lastError = null;
let currentDelay = retryDelay;
// Loop until retries are exhausted or successful
while (retryCount === "infinite" || currentRetryCount <= retryCount) {
try {
if (currentDelay > 0 && currentRetryCount > 0) {
yield sleep(currentDelay); // Wait for the delay if it's not the first attempt
}
const response = yield retryAsyncCallback();
onSuccessCallback(response); // Call success callback on success
return;
}
catch (error) {
lastError = error;
onErrorCallback(error, currentRetryCount); // Handle error with retry count
currentDelay *= incrementalDelayFactor; // Increase the delay for the next retry
currentRetryCount++;
}
}
// Call the final error callback if retries are exhausted
if (afterLastAttemptErrorCallback) {
afterLastAttemptErrorCallback(lastError);
}
});
}
exports.retryAsyncOperation = retryAsyncOperation;
//# sourceMappingURL=retryOperation.js.map