Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 views
in Technique[技术] by (71.8m points)

javascript - 异步箭头功能的语法(Syntax for async arrow function)

I can mark a javascript function as "async" (ie returning a promise) with the async keyword.

(我可以使用async关键字将javascript函数标记为“异步”(即返回承诺)。)

Like this:

(像这样:)

async function foo() {
  // do something
}

What is the equivalent syntax for arrow functions?

(箭头功能的等效语法是什么?)

  ask by BonsaiOak translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...