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));
Top comments (1)
`