[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Operators/await [Back]  [Original]

await - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

await

Baseline Widely available *

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2017 ..

* Some parts of this feature may have varying levels of support.

await Promise. async function .

In this article

[rv] = await expression;
expression

Promise .

rv

Promise , , Promise.

await , async, Promise Promise . Promise . , await, Promise, Promise.

Promise , await .

await Promise .

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();

Promise, Promise.

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

Promise , .

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

Promise try/catch .

js
var response = await promisedFunction().catch((err) => {
  console.log(err);
});
// response   undefined,  Promise  

Specification
ECMAScript 2027 LanguageSpecification
# sec-async-function-definitions


Web Proxy Viewer  |  New URL  |  Original Page