Home > database >  Javascript unexpected identifier but works in console
Javascript unexpected identifier but works in console

Time:05-26

When I create a script as such

    async function a(){
        return 'a';
    }
    console.log(await a());

The browser (Brave & Edge) gives the error Uncaught SyntaxError: missing ) after argument list

Funnily enough, copy and pasting the exact same code into the debug console runs without a complaint.

Any explanation?

CodePudding user response:

You can't use await the way you are using it. await should be only used inside of an async function.

try this:

async function a(){
        return 'a';
    }
    console.log(a());

CodePudding user response:

There is no need of using await here as we are not returning a promise here. instead try this.

    function a(){
       return 'a';
     }
    console.log(a());

or this if you are learning to use async await and want to use it anyway

async function a(){
    return 'a';
}
const value = await a()
console.log(value);
  • Related