Loading Depth Study
Preparing your workspace
The Fetch API allows browser JavaScript to make HTTP requests and process asynchronous responses.
Modern web applications frequently load or send data without performing a full page navigation. The Fetch API lets JavaScript make HTTP requests and handle the returned response. Developers need to understand request methods, headers, JSON bodies, response status codes, and asynchronous behavior. Network failures and non-successful HTTP responses should be handled deliberately.
A course dashboard can fetch the current user's progress from an API and update the interface without reloading the whole page.
async function loadCourses() {
const response = await fetch("/api/courses");
if (!response.ok) {
throw new Error("Failed to load courses");
}
const courses = await response.json();
console.log(courses);
}
loadCourses();The browser requests the courses endpoint, checks the HTTP response, parses the JSON data, and logs the courses.