Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork10.3k
Open
Description
We want to access the value returned from an asynchronous function using await, but it causes an error when used at the top level.
const square = async function (n) { return n * n;}const value = await square(2); // ❌await is only valid in async functions and the top level bodies of modulesconsole.log(value);
CORRECT APPROACH:
const square = async function (n) { return n * n;}async function main() { const value = await square(2); console.log(value); // 4}main();
Metadata
Metadata
Assignees
Labels
No labels