async function fetchWithRetry(url, retries = 3) {↵
for (let i = 0; i < retries; i++) {↵
try {↵
console.log(`Attempt ${i + 1}...`);↵
const response = await fakeFetch(url);↵
return response;↵
} catch (error) {↵
console.log(`Failed: ${error.message}`);↵
if (i === retries - 1) throw error;↵
await delay(1000);↵
}↵
}↵
}↵
↵
function delay(ms) {↵
return new Promise(r => setTimeout(r, ms));↵
}↵
↵
let attempts = 0;↵
function fakeFetch(url) {↵
return new Promise((resolve, reject) => {↵
attempts++;↵
if (attempts < 3) reject(new Error("Network error"));↵
else resolve({ data: "Success!" });↵
});↵
}↵
↵
fetchWithRetry("api/data")↵
.then(data => console.log(data))↵
.catch(err => console.log("Final error:", err));