Skip to content

Instantly share code, notes, and snippets.

@dfkaye
Created May 17, 2020 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfkaye/a4d71e12564c1874b3574ce1936500ea to your computer and use it in GitHub Desktop.
Save dfkaye/a4d71e12564c1874b3574ce1936500ea to your computer and use it in GitHub Desktop.
top-level await vs. async IIFE
// 17 May 2020
// top-level await vs. async IIFE
// Works in Chrome & Firefox
// Does not work in Edge (throws 'await' is not defined)
await (function() { return Promise.resolve('top-level await') })().then(console.warn);
// > "top-level await"
// async IIFE works across all 3 browsers here
// async goes inside the parentheses, else it's parsed as `async(fn)`
// which throws "ReferenceError: async is not defined"
(async function() {
return await (function() { return Promise.resolve('async IIFE') })();
})().then(console.warn);
// > "async IIFE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment