异步箭头功能的语法

JavaScript

Mandy小卤蛋凯

2020-03-13

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

async function foo() {
  // do something
}

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

第1513篇《异步箭头功能的语法》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

4个回答
Tom凯 2020.03.13

立即调用异步箭头功能:

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

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

(async function () {
    console.log(await asyncFunction());
})();
阳光卡卡西 2020.03.13

您也可以这样做:

 YourAsyncFunctionName = async (value) => {

    /* Code goes here */

}
米亚JinJin樱 2020.03.13

异步箭头函数如下所示:

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

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

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

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android