In this case, await can only be used within functions declared with the “async” keyword, using await in global scope or outside a function with “async” triggers the following error:
constmyPromise=newPromise((resolve)=>{resolve(‘payload’)});constresponse=awaitmyPromise;// SyntaxError: await is only valid in async function
I really believe that it is something related to the scope within the browser. Anyway, there is a proposal to implement “await” in the global scope: ECMAScript proposal: Top-level await, currently in phase 3, but it contains some interesting discussions about this implementation: Top-level await is a footgun 👣🔫.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
In this case, await can only be used within functions declared with the “async” keyword, using await in global scope or outside a function with “async” triggers the following error:
😁
That's interesting. In the console (Firefox) it worked fine. I guess that's a different scope again.
I really believe that it is something related to the scope within the browser. Anyway, there is a proposal to implement “await” in the global scope: ECMAScript proposal: Top-level await, currently in phase 3, but it contains some interesting discussions about this implementation: Top-level await is a footgun 👣🔫.