async-await
await
Pauses execution of the surrounding async function until the awaited promise settles, then resumes with its resolved value. If the promise rejects, await throws the reason, letting you use try/catch.
await expression Parameters
| Parameter | Purpose |
|---|---|
| value | Non-promise operands are wrapped and returned as-is |
| rejection | Rejected promises throw — catch with try/catch |
| context | Only valid inside async functions or at the top level of an ES module |
Examples
async function main() {
const res = await fetch('/api');
const data = await res.json();
console.log(data);
} Sequential awaits — each waits for the previous to complete.
// Parallel, not sequential
const [a, b] = await Promise.all([loadA(), loadB()]); Kick off both promises before awaiting to run them concurrently.
// Anti-pattern: sequential when you meant parallel
const a = await loadA();
const b = await loadB(); // waits for loadA first, wasted time Two consecutive awaits serialize the work — use Promise.all for independent tasks.
Gotcha
Using await inside a non-async function is a SyntaxError (except at the top level of an ES module). Awaiting sequentially on independent promises is a common performance bug — batch with Promise.all.
Related
async function
Declares a function that always returns a promise, letting you use await inside it to pause on other promises. A returned value fulfills the promise; a thrown error rejects it.
Top-level await
Allows using await outside of any async function at the top level of an ECMAScript module. The module's evaluation waits for the awaited promise, blocking importers until it settles.
try / catch with await
Since await throws when the awaited promise rejects, standard try/catch is the idiomatic way to handle async errors inside an async function. A finally block runs on both success and failure paths.
← All JS Promise reference · async/await vs promises vs callbacks