Send asynchronous browser requests with fetch, XMLHttpRequest, JSON, errors, and progressive UI.
Lessons
Write readable asynchronous request code.
async/await with fetch is easiest to learn by reading the example, changing it, and observing the result.
async function loadLessons() {
try {
const response = await fetch('/api/lessons');
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const data = await response.json();
return data;
} catch (error) {
console.error('Load failed:', error.message);
return [];
}
}Practice the async/await with fetch example in a small scratch file, then explain what changed and why.