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

async function foo() {
  // Do something
}

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


当前回答

异步箭头函数如下所示:

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

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

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

对于传递给它的多个参数,异步箭头函数如下所示:

const foo = async (evt, callback) => {
  // do something with evt
  // return response with callback
}

匿名表单也可以:

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

异步函数声明如下所示:

async function foo() {
  // do something
}

在回调中使用异步函数:

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

在类内部使用异步方法:

async foo() {
  // do something
}

其他回答

立即调用异步箭头函数:

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

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

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

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

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

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

static myFunction = async () => {
    // your code here
}
const asynchronousFunction = async () => {
  // do something;
  // await something else;
}

基本示例

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