await
await  操作符用于等待一个Promise对象。它只能在异步函数async function中使用。
语法
[rv] = await expression;表达式一个Promise对象或者任何要等待的值。返回值
返回 Promise 对象的处理结果。如果等待的不是 Promise 对象,则返回该值本身。
描述
await 表达式会暂停当前 async function 的执行,等待 Promise 处理完成。若 Promise 正常处理(fulfilled),其处理结果作为 await 表达式的值,继续执行 async function。
若 Promise 处理异常(rejected),await 表达式会把 Promise 的异常原因抛出。
例子
如果一个 Promise 被传递给一个 await 操作符,await 将等待 Promise 正常处理完成并返回其处理结果。
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,await 会把该值转换为已正常处理的Promise,然后等待其处理结果。
async function f2() {
  var y = await 20;
  console.log(y); // 20
}
f2();如果 Promise 处理异常,则异常值被抛出。
async function f3() {
  try {
    var z = await Promise.reject(30);
  } catch(e) {
    console.log(e); // 30
  }
}
f3();规范
| Specification | Status | Comment | 
|---|---|---|
| ECMAScript Latest Draft (ECMA-262)The definition of 'async functions' in that specification. | Living Standard | Initial definition in ES2017. | 
浏览器兼容性
| Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) | 
|---|---|---|---|---|---|---|
| Basic support | 55 | (Yes) | 52.0 (52.0) | ? | 42 | 10.1 | 
| Feature | Android Webview | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | 
|---|---|---|---|---|---|---|---|
| Basic support | 55 | 55 | (Yes) | 52.0 (52.0) | ? | 42 | 10.1 | 
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

