javascript – Syntax for async arrow function

The Question :

602 people think this question is useful

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?

The Question Comments :
  • It’s worth noting that at least firefox and babel does let you do that
  • var foo = async () => await Promise.resolve('ha'); – works just fine
  • saying it doesn't work is meaningless … are you getting an error? perhaps you’re doing something else wrong, without the code that “doesn’t work” and a meaningful description of how it doesn’t work, can only guess that you’re doing something wrong (or using an old browser)
  • that may well be @Pointy, but it does work natively in current firefox and chrome and node.js (7.7.4)
  • The ES2017 spec has a section on async arrow function definitions @Pointy.

The Answer 1

1016 people think this answer is useful

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

The Answer 2

137 people think this answer is useful

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

The Answer 3

60 people think this answer is useful

Immediately Invoked Async Arrow Function:

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

Immediately Invoked Async Function Expression:

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

The Answer 4

19 people think this answer is useful

Async Arrow function syntax with parameters

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

The Answer 5

19 people think this answer is useful

Basic Example

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

The Answer 6

14 people think this answer is useful

You may also do:

 YourAsyncFunctionName = async (value) => {

    /* Code goes here */

}

The Answer 7

3 people think this answer is useful

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

Add a Comment