Send asynchronous browser requests with fetch, XMLHttpRequest, JSON, errors, and progressive UI.
Lessons
Retry failed requests with exponential backoff.
Retry & Backoff is easiest to learn by reading the example, changing it, and observing the result.
async function fetchWithRetry(url, retries = 3, delay = 500) {
for (let attempt = 1; attempt <= retries; attempt++) {
try {
const res = await fetch(url);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return res.json();
} catch (error) {
if (attempt === retries) throw error;
await new Promise(r => setTimeout(r, delay * 2 ** (attempt - 1)));
}
}
}Practice the Retry & Backoff example in a small scratch file, then explain what changed and why.