Syntax for async arrow function

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

 async function foo() { // do something } 

What is the equivalent syntax for arrow functions?

+366
javascript promise arrow-functions async-await
Mar 22 '17 at 22:50
source share
6 answers

Asynchronous arrow functions look like this:

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

Asynchronous arrow functions look like this for one argument passed to it:

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

An anonymous form also works:

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

The declaration of an asynchronous function is as follows:

 async function foo() { // do something } 

Using an asynchronous function in a callback callback :

 const foo = event.onCall(async () => { // do something }) 
+615
Mar 22 '17 at 23:07 on
source share

This is the easiest way to assign an asynchronous arrow function to an expression of a variable named :

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

TA-dah!

(Note that this is not exactly equivalent to async function foo() { } . Apart from the differences between the function keyword and the arrow expression , the function in this answer does not “go up” ).

+107
Jun 04 '17 at 2:16 on
source share

Asynchronous arrow function immediately called up:

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

The immediately called expression of the asynchronous function:

 (async function () { console.log(await asyncFunction()); })(); 
+21
Jul 20 '18 at 14:04
source share

You can also do:

  YourAsyncFunctionName = async (value) => { /* Code goes here */ } 
+12
Dec 22 '17 at 19:08
source share

Asynchronous arrow function syntax with parameters

 const MyFunction = async (a,b,c) => { //code here } 
+6
Mar 26 '19 at 7:50
source share

/ * foo = async (props) => {

 /* Code goes here */ 

} Please delete these comments * /

-3
Jul 30 '19 at 2:48
source share



All Articles