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();
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();
asyncfunction should return a Promise as opposed to waiting for it to resolve:return Promise.resolve('John');. Then yourawait getName();call will have a Promise to wait for to be resolved.await is only valid in async functionI look like you are tryingconst name = await getName();outside of an sync function.await. You either need to wrap it in an async function or usegetName.then()