async function compactLog(fn, retryCount = 3, delay = 1000) {
let attempts = 0;
let success = false;
while (attempts < retryCount) {
try {
const result = await fn(); // Execute the function
console.log(`[${attempts + 1}/${retryCount}] ${result}`); // Compact log output
success = true;
break; // Exit loop on success
} catch (error) {
attempts++;
console.error(`[${attempts}/${retryCount}] Error: ${error.message}`); // Log errors
if (attempts < retryCount) {
await new Promise(resolve => setTimeout(resolve, delay)); // Wait before retrying
}
}
}
if (!success) {
console.error(`Failed after ${retryCount} attempts.`);
}
}
// Example usage (replace with your actual function)
async function myTask() {
// Simulate an asynchronous task that might fail
return new Promise(resolve => {
setTimeout(() => {
const random = Math.random();
if (random > 0.2) {
resolve("Task completed successfully!");
} else {
resolve("Task completed successfully!");
}
}, 500);
});
}
// Call the compactLog function
compactLog(myTask);
Add your comment