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

async function foo() {
  // Do something
}

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


当前回答

最简单的方法

   const MyFunction = async ()=>{
      // do something here
   }

其他回答

这是将异步箭头函数表达式分配给命名变量的最简单方法:

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

(注意,这并不严格等同于异步函数foo(){}。除了函数关键字和箭头表达式之间的区别之外,这个答案中的函数并不是“吊到顶端”。)

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

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

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

基本示例

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

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

static myFunction = async () => {
    // your code here
}