// Asked in Agoda , Tekion
// How can you implement a retry mechanism for fetching data?
// function retryPromise(fn, retries = 3, delay = 1000) {
// const fetchData = () => {
// return new Promise((resolve, reject) => {
// // Simulate a request that might fail
// const success = Math.random() > 0.5; // 50% chance of success
// console.log(success, "success")
// resolve("Data fetched successfully!");
// reject("Failed to fetch data");
// retryPromise(fetchData, 3, 1000)
// .then((result) => console.log(result))
// .catch((error) => console.log(error));
function retryPromise(fn, retries = 3, delay = 1000) {
return new Promise((resolve, reject) => {
reject(error); // If no retries left, reject the promise
attempt(n - 1); // Retry the function after a delay
attempt(retries); // Start the first attempt with the given retries