How to call an async arrow function with await

const getName = async () => await Promise.resolve('John');

In the above, you have an async function (the arrow function) which uses await inside.

This is fine (albeit pointless as you could return the promise directly).

Here:

const name = await getName();

You use await again, and while the function on the right-hand side does return a promise, the function it appears inside is not async so it is not valid.


Put it inside an async function and it will be fine:

const getName = async() => await Promise.resolve('John');

const init = async() => {
  const name = await getName();
  console.log(`Hi ${name}`);
};

init();

 Run code snippetExpand snippet

As mentioned though, making getName async and awaiting inside is just a needlessly complex set of nested promises and you could simplify:

const getName = () => Promise.resolve('John');

const init = async() => {
  const name = await getName();
  console.log(`Hi ${name}`);
};

init();

Leave a Comment