异步箭头功能的语法

我可以使用async关键字将javascript函数标记为“异步”(即返回承诺)像这样:

async function foo() {
  // do something
}

箭头功能的等效语法是什么?

Tom凯2020/03/13 18:14:32

立即调用异步箭头功能:

(async () => {
    console.log(await asyncFunction());
})();

立即调用异步函数表达式:

(async function () {
    console.log(await asyncFunction());
})();
阳光卡卡西2020/03/13 18:14:32

您也可以这样做:

 YourAsyncFunctionName = async (value) => {

    /* Code goes here */

}
米亚JinJin樱2020/03/13 18:14:32

异步箭头函数如下所示:

const foo = async () => {
  // do something
}

传递给它单个参数的异步箭头函数如下所示

const foo = async evt => {
  // do something with evt
}

The anonymous form works as well:

const foo = async function() {
  // do something
}

An async function declaration looks like this:

async function foo() {
  // do something
}

Using async function in a callback:

const foo = event.onCall(async () => {
  // do something
})
神奇启人2020/03/13 18:14:32

带参数的异步箭头函数语法

const myFunction = async (a, b, c) => {
   // Code here
}