1. async function compactLog(fn, retryCount = 3, delay = 1000) {
  2. let attempts = 0;
  3. let success = false;
  4. while (attempts < retryCount) {
  5. try {
  6. const result = await fn(); // Execute the function
  7. console.log(`[${attempts + 1}/${retryCount}] ${result}`); // Compact log output
  8. success = true;
  9. break; // Exit loop on success
  10. } catch (error) {
  11. attempts++;
  12. console.error(`[${attempts}/${retryCount}] Error: ${error.message}`); // Log errors
  13. if (attempts < retryCount) {
  14. await new Promise(resolve => setTimeout(resolve, delay)); // Wait before retrying
  15. }
  16. }
  17. }
  18. if (!success) {
  19. console.error(`Failed after ${retryCount} attempts.`);
  20. }
  21. }
  22. // Example usage (replace with your actual function)
  23. async function myTask() {
  24. // Simulate an asynchronous task that might fail
  25. return new Promise(resolve => {
  26. setTimeout(() => {
  27. const random = Math.random();
  28. if (random > 0.2) {
  29. resolve("Task completed successfully!");
  30. } else {
  31. resolve("Task completed successfully!");
  32. }
  33. }, 500);
  34. });
  35. }
  36. // Call the compactLog function
  37. compactLog(myTask);

Add your comment