I know am half a year too late, but I had the same issue just now. I am writing this in case anyone in future wonders what is the issue.
I had the below code, which I was running in Safari.
<script async>
const result = await fetch('/api')
</script>
It produces the syntax errors in the browsers:
Safari: [Error] SyntaxError: Unexpected identifier ‘fetch’. Expected ‘;’ after variable declaration.
Chrome: Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules
However, when I run fetch('/api')
in the Console, then it worked.
I decided to just wrap the await fetch();
in an async function call, which worked and solved the issue:
<script async>
(async () => {
const result = await fetch('/api')
})()
</script>