Day 19: Retry

1 min read ❤️ 0 · 💬 1
#day19 #100daysofcode #javascript

Question

Write a function retryAsyncOperation that takes an asynchronous operation represented as a Promise and retries it a certain number of times before rejecting the Promise.

function retryAsyncOperation(asyncFunction, retries, delay) {
    // Your code here
}

// Example usage
function simulateAsyncOperation() {
    return new Promise((resolve, reject) => {
        const success = Math.random() < 0.5; // Simulating success half of the time
        setTimeout(() => {
            if (success) {
                resolve("Success");
            } else {
                reject("Error");
            }
        }, 1000);
    });
}

retryAsyncOperation(simulateAsyncOperation, 3, 1000)
    .then(result => console.log("Result:", result))
    .catch(error => console.error("All attempts failed:", error));
Enter fullscreen mode Exit fullscreen mode

Originally published on DEV Community . Read the discussion and share your thoughts there.

© 2026

LinkedIn 𝕏 GitHub