/**
* Retries log entry operations with a fixed interval.
* @param {function} operation - The function to execute and potentially retry. Should return a Promise.
* @param {number} maxRetries - The maximum number of retry attempts.
* @param {number} initialDelay - The initial delay in milliseconds before the first retry.
*/
async function retryLogOperation(operation, maxRetries, initialDelay) {
let retryCount = 0;
while (retryCount < maxRetries) {
try {
const result = await operation(); // Execute the operation
console.log("Operation successful:", result); // Log success
return result; // Return the result if successful
} catch (error) {
retryCount++;
const delay = initialDelay * Math.pow(2, retryCount); // Exponential backoff
console.warn(`Operation failed. Retry attempt ${retryCount} in ${delay}ms.`);
await new Promise((resolve) => setTimeout(resolve, delay)); // Wait before retrying
}
}
console.error("Operation failed after", maxRetries, "retries.");
throw new Error("Operation failed after multiple retries"); // Throw an error if all retries fail
}
export default retryLogOperation;
Add your comment