This chapter follows Promises in JavaScript.
async function declares a function that implicitly returns a Promise. await pauses that function until a Promise settles, then resumes with the fulfilled value or throws the rejection reason.
async function loadUser(id) { const res = await fetch(`/api/users/${id}`); if (!res.ok) throw new Error(String(res.status)); return res.json();}Calling loadUser(1) returns a Promise—you can await loadUser(1) inside another async function or use .then.
Sequential versus parallel work
Await inside a loop runs steps one after another:
async function loadAllSequential(ids) { const users = []; for (const id of ids) { users.push(await loadUser(id)); } return users;}Independent requests should run together:
async function loadAllParallel(ids) { return Promise.all(ids.map((id) => loadUser(id)));}Sequential awaits when you depend on the previous result; Promise.all when you do not.
try/catch and finally
try/catch/finally wraps await the same way as sync code. Rejected promises from await throw into catch.
async function run() { try { const user = await loadUser(1); console.log(user.name); } catch (err) { console.error("Failed", err); } finally { console.log("done"); }}For patterns that attribute errors to specific steps—and TypeScript typing of results—see Error handling with async/await in TypeScript. Runtime throw and Error types are covered in Error handling in JavaScript.
Async arrow functions and methods
const save = async (data) => { await fetch("/api/save", { method: "POST", body: JSON.stringify(data) });};
class Store { async load() { return loadUser(this.id); }}Top-level await
ES modules may await at module top level. The module graph waits for that promise before importers run—useful for config, bad for hidden side effects on import.
When not to use async/await
Low-level promise combinators (race, any, custom then chains) sometimes stay clearer without await. for await..of is for async iterables, not for arbitrary promise lists—see Async iterators and for await..of.
Continue with Cancellation with AbortController.