异步箭头函数的语法 [英] Syntax for an async arrow function

查看:70
本文介绍了异步箭头函数的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将 JavaScript 函数标记为async";(即,返回承诺)与 async 关键字.像这样:

I can mark a JavaScript function as "async" (i.e., returning a promise) with the async keyword. Like this:

async function foo() {
  // Do something
}

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

What is the equivalent syntax for arrow functions?

推荐答案

异步箭头函数如下所示:

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
}

匿名表单也可以:

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

异步函数声明如下所示:

async function foo() {
  // do something
}

回调中使用异步函数:

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

使用异步方法:

async foo() {
  // do something
}

这篇关于异步箭头函数的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆