我可以用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
}

其他回答

我的异步函数

const getAllRedis = async (key) => {
  let obj = [];

  await client.hgetall(key, (err, object) => {
    console.log(object);
    _.map(object, (ob)=>{
      obj.push(JSON.parse(ob));
    })
    return obj;
    // res.send(obj);
});
}

基本示例

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

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

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

最简单的方法

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