patternjavascriptCritical
Syntax for an async arrow function
Viewed 0 times
arrowfunctionsyntaxforasync
Problem
I can mark a JavaScript function as "async" (i.e., returning a promise) with the
What is the equivalent syntax for arrow functions?
async keyword. Like this:async function foo() {
// Do something
}What is the equivalent syntax for arrow functions?
Solution
Async arrow functions look like this:
Async arrow functions look like this for a single argument passed to it:
Async arrow functions look like this for multiple arguments passed to it:
The anonymous form works as well:
An async function declaration looks like this:
Using async function in a callback:
Using async method inside of a class:
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
}
Context
Stack Overflow Q#42964102, score: 1636
Revisions (0)
No revisions yet.