Syntax for an async arrow function

JavascriptPromiseAsync AwaitArrow Functions

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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
}

Solution 2 - Javascript

This the simplest way to assign an async arrow function expression to a named variable:

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".)

Solution 3 - Javascript

Immediately Invoked Async Arrow Function:

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

Immediately Invoked Async Function Expression:

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

Solution 4 - Javascript

Async Arrow function syntax with parameters

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

Solution 5 - Javascript

Basic Example

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

Solution 6 - Javascript

async function foo() {
  // do something
}

Is equivalent to:

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

Calling foo with one argument like in the following example:

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)

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

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

if you call foo with two or more arguments

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:

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

Solution 7 - Javascript

You may also do:

 YourAsyncFunctionName = async (value) => {
 
    /* Code goes here */

}

Solution 8 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBonsaiOakView Question on Stackoverflow
Solution 1 - JavascriptBonsaiOakView Answer on Stackoverflow
Solution 2 - JavascriptEdoardo L'AstorinaView Answer on Stackoverflow
Solution 3 - JavascriptMichaelView Answer on Stackoverflow
Solution 4 - JavascriptcodemirrorView Answer on Stackoverflow
Solution 5 - JavascriptChaurasiaView Answer on Stackoverflow
Solution 6 - JavascriptRan TurnerView Answer on Stackoverflow
Solution 7 - JavascriptJustin E. SamuelsView Answer on Stackoverflow
Solution 8 - Javascriptsuraj gholapView Answer on Stackoverflow