简体   繁体   English

异步箭头的语法 function

[英]Syntax for an async arrow function

I can mark a JavaScript function as "async" (ie, returning a promise) with the async keyword.我可以使用async关键字将 JavaScript function 标记为“异步”(即返回承诺)。 Like this:像这样:

async function foo() {
  // Do something
}

What is the equivalent syntax for arrow functions?箭头函数的等效语法是什么?

Async arrow functions look like this:异步箭头函数如下所示:

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

Async arrow functions look like this for a single argument passed to it:对于传递给它的单个参数,异步箭头函数看起来像这样:

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

Async arrow functions look like this for multiple arguments passed to it:对于传递给它的多个参数,异步箭头函数看起来像这样:

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

The anonymous form works as well:匿名表单也可以:

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

An async function declaration looks like this:异步函数声明如下所示:

async function foo() {
  // do something
}

Using async function in a callback :回调中使用异步函数:

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

Using async method inside of a class :中使用异步方法

async foo() {
  // do something
}

This the simplest way to assign an async arrow function expression to a named variable:这是将async箭头函数表达式分配给命名变量的最简单方法:

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

(Note that this is not strictly equivalent to async function foo() { } . Besides the differences between the function keyword and an arrow expression , the function in this answer is not "hoisted to the top" .) (请注意,这并不严格等同于async function foo() { } 。除了function关键字和箭头表达式之间的区别之外,此答案中的函数不是“提升到顶部” 。)

Immediately Invoked Async Arrow Function:立即调用异步箭头函数:

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

Immediately Invoked Async Function Expression:立即调用异步函数表达式:

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

Async Arrow function syntax with parameters带参数的异步箭头函数语法

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

Basic Example基本示例

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

Is equivalent to:相当于:

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

Calling foo with one argument like in the following example:使用一个参数调用 foo,如下例所示:

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

Is equivalent to calling foo like this (both ways are acceptable since parentheses are optional but not required when just one argument is provided)等效于像这样调用 foo (两种方式都可以接受,因为括号是可选的,但在仅提供一个参数时不是必需的)

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

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

if you call foo with two or more arguments如果您使用两个或更多参数调用 foo

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

Is equivalent to: (parentheses are now required)相当于:(现在需要括号)

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

And for a practical example with an await use inside:对于内部使用 await 的实际示例:

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

You may also do:你也可以这样做:

 YourAsyncFunctionName = async (value) => {

    /* Code goes here */

}

My async function我的异步功能

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

Most simplest way最简单的方法

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

For a static async arrow function, it works like this:对于静态异步箭头函数,它的工作原理如下:

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

/*foo = async (props) => { / * foo =异步(props)=> {

/* Code goes here */

} Please remove these comments*/ },请删除这些评论* /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM