Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Operators

The await operator

Reference for the JavaScript await operator: awaiting promises, thenables and plain values, top-level await, error handling with try/catch and parallel patterns.

The await operator pauses an async function until a promise settles. If the promise fulfills, the await expression evaluates to the fulfillment value. If it rejects, the rejection reason is thrown at the await, so you can catch it with an ordinary try...catch. While the function is paused, the rest of the program keeps running; await never blocks the thread.

await is allowed inside async functions and at the top level of ES modules.

JavaScript
const value = await somePromise;

Try it

JS Await and error handling

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Sequential versus parallel; top-level await

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
await expression
Part Description
expression Usually a promise. Any other value (or a thenable) is converted with Promise.resolve() first.

Return value: the fulfillment value of the promise, or the value itself when expression is not a promise.

Exceptions: if the promise rejects, await throws the reason. Using await outside an async function or module is a SyntaxError (in a classic script, await is just an identifier).

Usage notes

  • Always a pause, even for ready values. await 1 still yields to the microtask queue, so code after it runs later than synchronous code.
  • Sequential by default. Two awaits in a row run one after the other. Start independent work first and await together with Promise.all().
  • await in loops. for...of with await processes items one at a time; array.forEach(async ...) does not wait for the callbacks. Use for...of or Promise.all(array.map(...)).
  • Error handling. Wrap awaits in try...catch, or attach .catch() to the promise. An unhandled rejection from an un-awaited promise is easy to miss.
  • Missing await is a common bug: the code receives a pending promise instead of the value. In try...catch, return promise without await also skips the catch block for that promise.
  • Precedence. await is a unary operator, so await a + b means (await a) + b. Use parentheses for (await fetchUser()).name.
  • Top-level await works in modules and delays modules that import the one using it.
  • Stack traces from async functions keep the awaited call chain in modern engines, but they are less complete than synchronous traces.

Browser support

await in async functions is supported in every current browser (ES2017). Top-level await in modules is also supported in current browsers (ES2022); check a compatibility table for older environments.