async function
async function 关键字用来在表达式中定义异步函数。当然,你也可以用 异步函数语句 来定义。
您还可以使用异步函数语句来定义异步函数。
语法
async function [name]([param1[, param2[, ..., paramN]]]) {
   statements
}从ES2015开始,您也可以使用箭头功能。
参数
name此异步函数的名称,可省略。如果省略则这个函数将成为匿名函数。该名称仅可在本函数中使用。
paramN传入函数的形参名称。
statements组成函数体的语句。
描述
示例
简单示例
function resolveAfter2Seconds(x) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve(x);
    }, 2000);
  });
};
var add = async function(x) { // async function expression assigned to a variable
  var a = await resolveAfter2Seconds(20);
  var b = await resolveAfter2Seconds(30);
  return x + a + b;
};
add(10).then(v => {
  console.log(v);  // prints 60 after 4 seconds.
});
(async function(x) { // async function expression used as an IIFE
  var p_a = resolveAfter2Seconds(20);
  var p_b = resolveAfter2Seconds(30);
  return x + await p_a + await p_b;
})(10).then(v => {
  console.log(v);  // prints 60 after 2 seconds.
});规范
| Specification | Status | Comment | 
|---|---|---|
| ECMAScript Latest Draft (ECMA-262)The definition of 'async function' in that specification. | Living Standard | Initial definition in ES2017. | 
浏览器兼容性
| Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) | 
|---|---|---|---|---|---|---|
| Basic support | 55 | ? | 52.0 (52.0) | ? | 42 | ? | 
| Feature | Android | Android Webview | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android | 
|---|---|---|---|---|---|---|---|---|
| Basic support | ? | ? | ? | 52.0 (52.0) | ? | 42 | ? | 55 | 
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

