Advertisement
728x90
Prior to ES2022, if you attempted to use an await outside of an async function resulted in a SyntaxError.
javascript
1await Promise.resolve(console.log("Hello await")); // SyntaxError: await is only valid in async functionBut you can fix this issue with an alternative IIFE (Immediately Invoked Function Expression) to get access to the feature.
javascript
1(async function () {
2
3await Promise.resolve(console.log("Hello await")); // Hello await
4})();In ES2022, you can write top-level await without writing any hacks.
javascript
1await Promise.resolve(console.log("Hello await")); //Hello awaitAdvertisement
Responsive Ad
🎯 Practice NowRelated Challenge
JavaScript Coding Exercise 22
Test your knowledge with this interactive coding challenge.
Start CodingAdvertisement
728x90
451of476