我可以用async关键字将JavaScript函数标记为“async”(即返回promise)。这样地:

async function foo() {
  // Do something
}

箭头函数的等效语法是什么?


当前回答

const asynchronousFunction = async () => {
  // do something;
  // await something else;
}

其他回答

基本示例

folder = async () => {
    let fold = await getFold();
    //await localStorage.save('folder');
    return fold;
  };

立即调用异步箭头函数:

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

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

(async function () {
    console.log(await asyncFunction());
})();

对于静态异步箭头函数,其工作方式如下:

static myFunction = async () => {
    // your code here
}
async function foo() {
  // do something
}

相当于:

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

使用一个参数调用foo,如下例所示:

async function foo(arg1) {
  // do something
}

相当于这样调用foo(这两种方式都是可以接受的,因为括号是可选的,但当只提供一个参数时不需要)

const foo = async arg1 => {
  // do something
}

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

如果使用两个或多个参数调用foo

async function foo(arg1, arg2) {
  // do something
}

相当于:(现在需要括号)

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

对于一个内部有等待使用的实际示例:

const foo = async () => await Promise.resolve('done');

您还可以执行以下操作:

 YourAsyncFunctionName = async (value) => {

    /* Code goes here */

}