Promises and async/await
Promises and async/await
A Promise represents a future result. async functions always return promises; await pauses that function until a promise settles. Use try/catch to handle failures and avoid blocking the browser with long synchronous work.
Try it yourself
Change the example, run it, and explain the result in your own words. Then create one variation of your own.
Key idea
Use the example as a starting point, not something to memorize. Practise, inspect the result and make small improvements.
Code example
async function loadData() {
try {
const response = await fetch("/data.json");
if (!response.ok) throw new Error("Request failed");
return await response.json();
} catch (error) { console.error(error); }
}