await

Pauses an async function until a promise settles, then returns its value or throws its error.

Returns
The value of the fulfilled promise. On rejection, it throws an error.
On this page

Example

#
JavaScript
const wait = (ms, value) => new Promise((resolve) => setTimeout(() => resolve(value), ms))

console.log('Fetching the test result...')
const score = await wait(300, 87)
console.log(`Score: ${score}%`)
Console
Fetching the test result...
Score: 87%

Definition and usage

#

You place the await operator in front of a promise. The async function pauses at that point, and once the promise is fulfilled, await returns its value and the code continues. If the promise is rejected, await throws an error that you catch with a try...catch block.

Only the current function pauses, not the whole page. Meanwhile, the browser keeps handling clicks, animations, and other code. await works inside async functions, and also at the top level of JavaScript modules, as in the examples on this page.

Successive await statements run one after another. If the operations don’t depend on each other, start them together and wait for all of them with await Promise.all([...]). The whole thing then takes as long as the longest operation, not the sum of their times.

Syntax

#
Syntax
const result = await promise

Parameters

#
  • promise

    a promise or any value

    The promise you’re waiting for. A plain value is returned unchanged.

More examples

#
Handling errors with try...catch
JavaScript
const fail = (ms, message) =>
  new Promise((_, reject) => setTimeout(() => reject(new Error(message)), ms))

try {
  await fail(200, 'Session expired')
  console.log('You won’t see this')
} catch (error) {
  console.log('Error:', error.message)
}
Console
Error: Session expired
One after another or in parallel
JavaScript
const task = (name, ms) =>
  new Promise((resolve) => {
    console.log(`start: ${name}`)
    setTimeout(() => {
      console.log(`end: ${name}`)
      resolve(name)
    }, ms)
  })

console.log('One after another')
await task('profile', 200)
await task('courses', 100)

console.log('In parallel')
await Promise.all([task('profile', 200), task('courses', 100)])
Console
One after another
start: profile
end: profile
start: courses
end: courses
In parallel
start: profile
start: courses
end: courses
end: profile

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Don’t forget await before calling an async function. Without it you get a promise instead of a result, and its error won’t reach your try...catch.
  • Start independent operations together and wait for them with await Promise.all([...]) instead of writing several await statements in a row.
  • await won’t work in a regular function or in a function passed to forEach(). For a loop that waits, use for...of inside an async function.

Related entries

#

Spotted a mistake or missing an example? Write to us.