I have very simple code as below .
Function ls has async keyword and it returns Promise.
But , calling const val = await ls()
gives below error.
SyntaxError: await is only valid in async functions and the top level bodies of modules
Can anybody pleasse help me why this error is coming as :
- The function ls has async keyword
- It also returns a Promise
Further more , using then clause like below , it works fine
ls().then(val => console.log(val)).catch(e => e)
But below code doesn't work
async function ls() {
return new Promise((resolve) => {
resolve('Print Output !!')
});
}
const val = await ls()
console.log(val)