await

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2017.

O operador await é utilizado para esperar por uma Promise. Ele pode ser usado apenas dentro de uma async function.

Sintaxe

[rv] = await expressão;
expressão

Uma Promise ou qualquer valor para esperar uma resolução.

rv

Retorna um valor final da promise, ou o próprio valor se ele não for uma Promise.

Descrição

A expressão await faz a execução de uma função async pausar, para esperar pelo retorno da Promise, e resume a execução da função async quando o valor da Promise é resolvido. Ele então retorna o valor final da Promise. Se esse valor não for uma Promise, ele é convertido para uma Promise resolvida.

Se a Promise for rejeitada, a expressão await invoca uma Exception com o valor rejeitado.

Exemplos

Se uma Promise é passada para uma expressão await, ele espera pela sefinalização da Promise e retorna seu valor final.

js
function resolveAfter2Seconds(x) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(x);
    }, 2000);
  });
}

async function f1() {
  var x = await resolveAfter2Seconds(10);
  console.log(x); // 10
}
f1();

Se o valor não for uma Promise, ele converte o valor para uma Promise resolvida, e espera por ele.

js
async function f2() {
  var y = await 20;
  console.log(y); // 20
}
f2();

Se a Promise for rejeitada, o valor rejeitado é invocado em uma Exception.

js
async function f3() {
  try {
    var z = await Promise.reject(30);
  } catch (e) {
    console.log(e); // 30
  }
}
f3();

Especificações

Specification
ECMAScript® 2025 Language Specification
# sec-async-function-definitions

Compatibilidade com navegadores

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
await
Use at module top level

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
See implementation notes.

Veja também