Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Top level await not working in ECMAScript

I am rather confused as I have read that top level await is supported in ESM but when I try it out in a html file it does not work?

Were I found that is says:
Top-level await does not work with node 14.13.-
"Top-level await only works with ESM modules"

Is top level await supported in ESM and if how can I use it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks in advanced!

>Solution :

Yes, it’s supported in ESM – ES6 modules. It’s not supported in plain script tags.

<script>
await Promise.resolve();
</script>

You need to specify that the script is a module for it to work.

<script type="module">
await Promise.resolve();
console.log('finished successfully');
</script>

(Also make sure that you’re running this in a supported environment – older environments may not support top-level await)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading