Preparing your workspace
Asynchronous JavaScript allows the program to start operations that finish later without blocking the entire application.
Web applications often need to wait for network requests, timers, files, or other operations. Promises represent the eventual result of an asynchronous operation. async functions always return promises, and await makes promise-based code easier to read by allowing the function to pause until the awaited promise settles. fetch is commonly used to request data from APIs. Asynchronous code should handle both successful results and failures. Understanding asynchronous flow is essential for modern frontend and backend development.
A learning dashboard may request a student's course progress from an API while the page loads.
async function loadProgress() {
try {
const response = await fetch('/api/progress');
if (!response.ok) {
throw new Error('Failed to load progress');
}
const data = await response.json();
console.log(data);
} catch (error) {
console.error(error);
}
}
loadProgress();The application requests progress data and prints the returned data when the request succeeds.