async/await
async functions always return a Promise. await pauses execution until the Promise settles, making async code read like synchronous code.
async functions always return a Promise. await pauses execution until the Promise settles, making async code read like synchronous code.
async function loadUser(id) {
try {
const res = await fetch(`/api/users/${id}`);
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return await res.json();
} catch (err) {
console.error("Load failed:", err);
throw err;
}
}
await can only be used inside an async function (or at module top-level in modern environments).
More in JavaScript