1. /**
  2. * Retries log entry operations with a fixed interval.
  3. * @param {function} operation - The function to execute and potentially retry. Should return a Promise.
  4. * @param {number} maxRetries - The maximum number of retry attempts.
  5. * @param {number} initialDelay - The initial delay in milliseconds before the first retry.
  6. */
  7. async function retryLogOperation(operation, maxRetries, initialDelay) {
  8. let retryCount = 0;
  9. while (retryCount < maxRetries) {
  10. try {
  11. const result = await operation(); // Execute the operation
  12. console.log("Operation successful:", result); // Log success
  13. return result; // Return the result if successful
  14. } catch (error) {
  15. retryCount++;
  16. const delay = initialDelay * Math.pow(2, retryCount); // Exponential backoff
  17. console.warn(`Operation failed. Retry attempt ${retryCount} in ${delay}ms.`);
  18. await new Promise((resolve) => setTimeout(resolve, delay)); // Wait before retrying
  19. }
  20. }
  21. console.error("Operation failed after", maxRetries, "retries.");
  22. throw new Error("Operation failed after multiple retries"); // Throw an error if all retries fail
  23. }
  24. export default retryLogOperation;

Add your comment