SyntaxStudy
Sign Up
JavaScript Beginner 4 min read

async/await

async/await

async functions always return a Promise. await pauses execution until the Promise settles, making async code read like synchronous code.

Example
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;
  }
}
Pro Tip

await can only be used inside an async function (or at module top-level in modern environments).