async function getUser(id) {
try {
const res = await fetch(`/api/users/${id}`);
if (!res.ok) throw new Error('Request failed');
const user = await res.json();
return user;
} catch (err) {
console.error('Failed to load user:', err);
throw err;
}
}- async functions always return a Promise
- await pauses execution until the awaited Promise settles
- await only works inside an async function (or top-level modules)
- Wrap awaits in try/catch to handle rejected promises
Tip
Mention that async/await doesn't block the main thread — it's still non-blocking under the hood.