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

async function foo() {
  // Do something
}

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


当前回答

立即调用异步箭头函数:

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

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

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

其他回答

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

立即调用异步箭头函数:

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

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

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

您还可以执行以下操作:

 YourAsyncFunctionName = async (value) => {

    /* Code goes here */

}

我的异步函数

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);
});
}

最简单的方法

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