{"id":13670200,"url":"https://github.com/tc39/proposal-top-level-await","last_synced_at":"2025-09-27T07:32:11.257Z","repository":{"id":51039425,"uuid":"118504771","full_name":"tc39/proposal-top-level-await","owner":"tc39","description":"top-level `await` proposal for ECMAScript (stage 4)","archived":true,"fork":false,"pushed_at":"2021-05-28T08:02:18.000Z","size":646,"stargazers_count":1084,"open_issues_count":0,"forks_count":46,"subscribers_count":56,"default_branch":"main","last_synced_at":"2024-12-27T21:02:41.167Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://tc39.github.io/proposal-top-level-await/","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tc39.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-22T19:32:45.000Z","updated_at":"2024-11-10T08:14:37.000Z","dependencies_parsed_at":"2022-09-05T10:30:26.264Z","dependency_job_id":null,"html_url":"https://github.com/tc39/proposal-top-level-await","commit_stats":null,"previous_names":["mylesborins/proposal-top-level-await"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tc39%2Fproposal-top-level-await","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tc39%2Fproposal-top-level-await/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tc39%2Fproposal-top-level-await/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tc39%2Fproposal-top-level-await/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tc39","download_url":"https://codeload.github.com/tc39/proposal-top-level-await/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234410346,"owners_count":18828186,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T09:00:35.922Z","updated_at":"2025-09-27T07:32:05.014Z","avatar_url":"https://github.com/tc39.png","language":"HTML","funding_links":[],"categories":["HTML"],"sub_categories":[],"readme":"# ECMAScript proposal: Top-level `await`\n\nChampions: Myles Borins, Yulia Startsev.\n\nAuthors: Myles Borins, Yulia Startsev, Daniel Ehrenberg, Guy Bedford, Ms2ger, and others.\n\nStatus: Stage 4\n\n## Synopsis\n\nTop-level `await` enables modules to act as big async functions: With top-level `await`, ECMAScript Modules (ESM) can `await` resources, causing other modules who `import` them to wait before they start evaluating their body.\n\n## Motivation\n\n### Limitations on IIAFEs\n\nWith `await` only available within `async` functions, a module can include an `await` in the code that executes at startup by factoring that code into an `async` function:\n\n```mjs\n// awaiting.mjs\nimport { process } from \"./some-module.mjs\";\nlet output;\nasync function main() {\n  const dynamic = await import(computedModuleSpecifier);\n  const data = await fetch(url);\n  output = process(dynamic.default, data);\n}\nmain();\nexport { output };\n```\n\nThis pattern can also be immediately invoked. You could call this an Immediately Invoked Async Function Expression (IIAFE), as a play on [IIFE](https://developer.mozilla.org/en-US/docs/Glossary/IIFE) idiom.\n\n```mjs\n// awaiting.mjs\nimport { process } from \"./some-module.mjs\";\nlet output;\n(async () =\u003e {\n  const dynamic = await import(computedModuleSpecifier);\n  const data = await fetch(url);\n  output = process(dynamic.default, data);\n})();\nexport { output };\n```\n\nThis pattern is appropriate for situations where loading a module is intended to schedule work that will happen some time later. However, the exports from this module may be accessed before this async function completes: If another module imports this one, it may see `output` as `undefined`, or it may see it after it's initialized to the return value of `process`, depending on when the access occurs! For example:\n\n```mjs\n// usage.mjs\nimport { output } from \"./awaiting.mjs\";\nexport function outputPlusValue(value) { return output + value; }\n\nconsole.log(outputPlusValue(100));\nsetTimeout(() =\u003e console.log(outputPlusValue(100), 1000);\n```\n\n### Workaround: Export a Promise to represent initialization\n\nIn the absence of this feature, it's possible to export a Promise from a module, and wait on that to know when its exports are ready. For example, the above module could be written as:\n\n```mjs\n// awaiting.mjs\nimport { process } from \"./some-module.mjs\";\nlet output;\nexport default (async () =\u003e {\n  const dynamic = await import(computedModuleSpecifier);\n  const data = await fetch(url);\n  output = process(dynamic.default, data);\n})();\nexport { output };\n```\n\nThen, the module could be used as:\n\n```mjs\n// usage.mjs\nimport promise, { output } from \"./awaiting.mjs\";\nexport function outputPlusValue(value) { return output + value }\n\npromise.then(() =\u003e {\n  console.log(outputPlusValue(100));\n  setTimeout(() =\u003e console.log(outputPlusValue(100), 1000);\n});\n```\n\nHowever, this leaves us with a number of problems still:\n- Everyone has to learn about a particular protocol to find the right Promise to wait on the module being loaded\n- If you forget to apply the protocol, things might \"just work\" some of the time (due to the race being won in a certain way)\n- In a deep module hierarchy, the Promise needs to be explicitly threaded through each step of the chain.\n\nFor example, here, we waited on the promise from `\"./awaiting.mjs\"` properly, but we forgot to re-export it, so modules that use our module may still run into the original race condition.\n\n### Avoiding the race through significant additional dynamism\n\nTo avoid the hazard of forgetting to wait for the exported Promise before accessing exports, a module could instead export a Promise which resolves to an object which contains exports\n\n```mjs\n// awaiting.mjs\nimport { process } from \"./some-module.mjs\";\nexport default (async () =\u003e {\n  const dynamic = await import(computedModuleSpecifier);\n  const data = await fetch(url);\n  const output = process(dynamic.default, data);\n  return { output };\n})();\n```\n\n```mjs\n// usage.mjs\nimport promise from \"./awaiting.mjs\";\n\nexport default promise.then(({output}) =\u003e {\n  function outputPlusValue(value) { return output + value }\n\n  console.log(outputPlusValue(100));\n  setTimeout(() =\u003e console.log(outputPlusValue(100), 1000);\n\n  return { outputPlusValue };\n});\n```\n\nIt's unclear whether this pattern has caught on, but it's sometimes [recommended in StackOverflow](https://stackoverflow.com/questions/42958334/how-can-i-export-promise-result/42958644#42958644) to people who face these sorts of issues.\n\nHowever, this pattern has the undesirable effect of requiring a broad reorganization of the related source into more dynamic patterns, and placing much of the module body inside the `.then()` callback in order to use the dynamically available imports. This represents a significant regression in terms of static analyzability, testability, ergonomics and more, compared to ES2015 modules. And when you run into a deep dependency which needs to `await`, you need to reorganize all dependent modules to use this pattern.\n\n### Solution: Top-level `await`\n\nTop-level `await` lets us rely on the module system itself to handle all of these promises, and make sure that things are well-coordinated. The above example could be simply written and used as follows:\n\n```mjs\n// awaiting.mjs\nimport { process } from \"./some-module.mjs\";\nconst dynamic = import(computedModuleSpecifier);\nconst data = fetch(url);\nexport const output = process((await dynamic).default, await data);\n```\n\n```mjs\n// usage.mjs\nimport { output } from \"./awaiting.mjs\";\nexport function outputPlusValue(value) { return output + value }\n\nconsole.log(outputPlusValue(100));\nsetTimeout(() =\u003e console.log(outputPlusValue(100), 1000);\n```\n\nNone of the statements in `usage.mjs` will execute until the `await`s in `awaiting.mjs` have had their Promises resolved, so the race condition is avoided by design. This is an extension of how, if `awaiting.mjs` didn't use top-level `await`, none of the statements in `usage.mjs` will execute until `awaiting.mjs` is loaded and all of its statements have executed.\n\n## Use cases\n\nWhen would it make sense to have a module which waits on an asynchronous operation to load? This section gives some examples.\n\n### Dynamic dependency pathing\n\n```mjs\nconst strings = await import(`/i18n/${navigator.language}`);\n```\n\nThis allows for Modules to use runtime values in order to determine\ndependencies. This is useful for things like development/production splits,\ninternationalization, environment splits, etc.\n\n### Resource initialization\n\n```mjs\nconst connection = await dbConnector();\n```\n\nThis allows Modules to represent resources and also to produce errors in \ncases where the Module will never be able to be used.\n\n### Dependency fallbacks\n\n```mjs\nlet jQuery;\ntry {\n  jQuery = await import('https://cdn-a.com/jQuery');\n} catch {\n  jQuery = await import('https://cdn-b.com/jQuery');\n}\n```\n\n### WebAssembly Modules\n\nWebAssembly Modules are \"compiled\" and \"instantiated\" in a logically asynchronous way, based on their imports: Some WebAssembly implementations do nontrivial work at either phase, which is important to be able to shunt off into another thread. To integrate with the JavaScript module system, they will need to do the equivalent of a top-level await. See the [WebAssembly ESM integration proposal](https://github.com/webassembly/esm-integration) for more details.\n\n## Semantics as desugaring\n\nCurrently, a module waits for all of its dependencies to execute all of their statements before the import is considered finished, and the module's code can run. This proposal maintains this property when introducing `await`, : dependencies still execute through to the end, even if you need to wait for that execution to finish asynchronously. One way to think of this is as if each module exported a Promise, and after all the `import` statements, but before the rest of the module, the Promises are all `await`ed:\n\n```mjs\nimport { a } from './a.mjs';\nimport { b } from './b.mjs';\nimport { c } from './c.mjs';\n\nconsole.log(a, b, c);\n```\n\nwould be roughly equivalent to\n\n```mjs\nimport { promise as aPromise, a } from './a.mjs';\nimport { promise as bPromise, b } from './b.mjs';\nimport { promise as cPromise, c } from './c.mjs';\n\nexport const promise = Promise.all([aPromise, bPromise, cPromise]).then(() =\u003e {\n\nconsole.log(a, b, c);\n\n});\n```\n\nModules `a.mjs`, `b.mjs`, and `c.mjs` would all execute in order up until the first await in each of them; we then wait on all of them to resume and finish evaluating before continuing.\n\n### FAQ\n\n#### Isn't top-level `await` a footgun?\n\nIf you have seen [the gist](https://gist.github.com/Rich-Harris/0b6f317657f5167663b493c722647221) you likely have heard this critique before.\n\nSome responses to some of the top concerns here:\n\n##### Will top-level `await` cause developers to make their code block longer than it should?\n\nIt's true that top-level `await` gives developers a new tool to make their code wait. Our hope is that proper developer education can ensure that the semantics of top-level `await` are well-understood, so that people know to use it just when they intend that importers should block on it.\n\nWe've seen this work well in the past. For example, it's easy to write code with async/await that serializes two tasks that could be done in parallel, but a deliberate developer education effort has popularized the use of `Promise.all` to avoid this hazard.\n\n##### Will top-level `await` encourage developers to use `import()` unnecessarily, which is less optimizable?\n\nMany JavaScript developers are learning about `import()` specifically as a tool for code splitting. People are becoming aware of the relationship between bundling and multiple requests, and learning how to combine them for good application performance. Top-level `await` doesn't really change the calculus--using `import()` from a top-level `await` will have similar performance effects to using it from a function. As long as we can tie top-level `await`'s educational materials into the existing knowledge of that performance tradeoff, we hope to be able to avoid counterproductive increases in the use of `import()`.\n\n#### What exactly is blocked by a top-level `await`?\n\nWhen one module imports another one, the importing module will only start executing its module body once the dependency's body has finished executing. If the dependency reaches a top-level await, that will have to complete before the importing module's body starts executing.\n\n#### Why doesn't top-level `await` block the import of an adjacent module?\n\nIf one module wants to declare itself dependent on another module, for the purposes of waiting for that other module to complete its top-level `await` statements before the module body executes, it can declare that other module as an import.\n\nIn a case such as the following, the printed order will be `\"X1\"`, `\"Y\"`, `\"X2\"`, because importing one module \"before\" another does not create an implicit dependency.\n\n```mjs\n// x.mjs\nconsole.log(\"X1\");\nawait new Promise(r =\u003e setTimeout(r, 1000));\nconsole.log(\"X2\");\n```\n\n```mjs\n// y.mjs\nconsole.log(\"Y\");\n```\n\n```mjs\n// z.mjs\nimport \"./x.mjs\";\nimport \"./y.mjs\";\n```\n\nDependencies are required to be explicitly noted in order to boost the potential for parallelism: Most setup work that will be blocking due to a top-level await (for example, all of the case studies above) can be done in parallel with other setup work from unrelated modules. When some of this work may be highly parallelizable (e.g., network fetches), it's important to get as many of these queued up close to the start of execution as possible.\n\n#### What is guaranteed about code execution order?\n\nModules maintain the same ordering as in ES2015 for when they start executing. If a module reaches an `await`, it will yield control and let other modules initialize themselves in the same well-specified order.\n\nTo be specific: Regardless of whether top-level `await` is used, modules always initially start running in the same post-order traversal established in ES2015: execution of module bodies starts with the deepest imports, in the order that the import statements for them are reached. After a top-level `await` is reached, control is passed to start the next module in this traversal order, or to other asynchronously scheduled code.\n\n#### Do these guarantees meet the needs of polyfills?\n\nCurrently (in a world without top-level `await`), polyfills are synchronous. So, the idiom of importing a polyfill (which modifies the global object) and then importing a module which should be affected by the polyfill will still work if top-level `await` is added. However, if a polyfill includes a top-level `await`, it will need to be imported by modules that depend on it in order to reliably take effect.\n\n#### Does the `Promise.all` happen even if none of the imported modules have a top-level `await`?\n\nIf the module's execution is deterministically synchronous (that is, if it and its dependencies each contain no top-level `await`), there will be no entry in the `Promise.all` for that module. In this case, it will run synchronously.\n\nThese semantics preserve the current behavior of ES Modules, where, when top-level `await` is not used, the Evaluate phase is entirely synchronous. The semantics are a bit in contrast with uses of Promises elsewhere. For a concrete example and further discussion, see [issue #43](https://github.com/tc39/proposal-top-level-await/issues/43) and [#47](https://github.com/tc39/proposal-top-level-await/issues/47).\n\n#### How exactly are dependencies waited on? Does it really use `Promise.all`?\n\nThe semantics of modules without top-level `await` is synchronous: the whole tree executes in postorder, with a module running just after its dependencies have run. The same semantics apply to modules with top-level `await`: Once the module which contains top-level `await` has executed, it will trigger the synchronous execution of dependent modules whose dependencies have all executed. If a module does contain top-level `await`, even if the `await` is not dynamically reached, the whole module will be treated as \"asynchronous\", as if it were a big async function. Therefore, anything that runs when it's done is in a Promise reaction. However, from there, if there are multiple modules which are dependent on it, and these do not contain top-level await, then they will run synchronously, *without* any Promise work between them.\n\n#### Does top-level `await` increase the risk of deadlocks?\n\nTop-level `await` creates a new mechanism for deadlocks, but the champions of this proposal consider the risk to be worth it, because:\n- There are many existing ways that modules can create deadlocks or otherwise halt progress, and developer tools can help in debugging them\n- All deterministic deadlock prevention strategies considered would be overly broad and block appropriate, realistic, useful patterns\n\n##### Existing Ways to block progress\n\n###### Infinite Loops\n\n```mjs\nfor (const n of primes()) {\n  console.log(`${n} is prime}`);\n}\n```\n\nInfinite series or lack of base condition means static control structures\nare vulnerable to infinite looping.\n\n###### Infinite Recursion\n\n```mjs\nconst fibb = n =\u003e (n ? fibb(n - 1) : 1);\nfibb(Infinity);\n```\n\nProper tail calls allow for recursion to never overflow the stack. This makes\nit vulnerable to infinite recursion.\n\n###### Atomics.wait\n\n```mjs\nAtomics.wait(shared_array_buffer, 0, 0);\n```\n\nAtomics allow blocking forward progress by waiting on an index that never changes.\n\n###### `export function then`\n\n```mjs\n// a\nexport function then(f, r) {}\n```\n\n```mjs\nasync function start() {\n  const a = await import('a');\n  console.log(a);\n}\n```\n\nExporting a `then` function allows blocking `import()`.\n\n###### Conclusion: Ensuring continued progress is a larger problem\n\n##### Rejected deadlock prevention mechanisms\n\nA potential problem space to solve for in designing top level await is to aid in detecting and preventing forms of deadlock that can occur. For example awaiting on a cyclical dynamic import could introduce deadlock into the module graph execution.\n\nThe following sections about deadlock prevention will be based on this code example:\n\n```jsx\n// file.html\n\u003cscript type=module src=\"a.mjs\"\u003e\u003c/script\u003e\n\n// a.mjs\nawait import(\"./b.mjs\");\n\n// b.mjs\nawait import(\"./a.mjs\");\n```\n\n###### Alternative: Return a partially filled module record\n\nIn `b.mjs`, resolve the Promise immediately, even though `a.mjs` has not yet completed, to avoid a deadlock.\n\n###### Alternative: Throw an exception on use of in-progress modules\n\nIn `b.mjs`, reject the Promise when importing `a.mjs` because that module hasn't completed yet, to prevent a deadlock.\n\n###### Case study: Race to `import()` a module\n\nBoth of these strategies fall over when considering that multiple pieces of code may want to dynamically import the same module. Such multiple imports would not ordinarily be any sort of race or deadlock to worry about. However, neither of the above mechanisms would handle the situation well: One would reject the Promise, and the other would fail to wait for the imported module to be initialized.\n\n###### Conclusion: No feasible strategy for deadlock avoidance\n\n#### Will top-level await work in transpilers?\n\nTo the greatest extent possible. The widely deployed CommonJS (CJS) module system does not directly support top-level await, so any transpilation strategy targeting it will need adjustments. However, within this context, we've made several adjustments to the semantics of top-level await based on the feedback and experience of the authors of several JavaScript module systems, including transpiler authors. This proposal aims to be implementable in such contexts.\n\n#### Without this proposal, module graph execution is synchronous. Does this proposal maintain developer expectations that such loading be synchronous?\n\nTo the greatest extent possible. When a module includes a top-level `await` (even if that `await` is not dynamically reached), this is not synchronous, and at the very least takes one trip through the Promise job queue. However, module subgraphs which do not use top-level await continue to run synchronously in exactly the same way as without this proposal. And if several modules which do not use top-level `await` depend on a module which does use it, then those modules will all run when the async module is ready, without yielding to any other work (neither the Promise job queue/microtask queue, nor the host's event loop, etc.). See [#74](https://github.com/tc39/proposal-top-level-await/pull/74) for details on the logic used.\n\n#### Should module loading include microtask checkpoints between modules, or yielding to the event loop after modules load?\n\nMaybe! These module loading questions are part of an exciting research area in loading performance, as well as an interesting discussion on the invariants surrounding microtask checkpoints. This proposal doesn't take an opinion on these questions, leaving the asynchronous behavior for separate proposals. Host environments may wrap modules in a way which does these things, and the top-level await specification machinery can be used to coordinate things. A future proposal either in TC39 or in a host environment could add additional microtask checkpoints. For related discussion, see [whatwg/html#4400](https://github.com/whatwg/html/issues/4400).\n\n#### Would top-level await work in web pages?\n\nYes. The details of the integration into the HTML specification is proposed at [whatwg/html#4352](https://github.com/whatwg/html/pull/4352).\n\n## History\n\n[The `async` / `await` proposal](https://github.com/tc39/ecmascript-asyncawait) was originally brought to committee in [January of 2014](https://github.com/tc39/tc39-notes/blob/master/meetings/2014-01/jan-30.md#asyncawait). In [April of 2014](https://github.com/tc39/tc39-notes/blob/master/meetings/2014-04/apr-10.md#preview-of-asnycawait) it was discussed that the keyword `await` should be reserved in the module goal for the purpose of top-level `await`. In [July of 2015](https://github.com/tc39/tc39-notes/blob/master/meetings/2015-07/july-30.md#64-advance-async-functions-to-stage-2) [the `async` / `await` proposal](https://github.com/tc39/ecmascript-asyncawait) advanced to Stage 2. During this meeting it was decided to punt on top-level `await` to not block the current proposal as top-level `await` would need to be \"designed in concert with the loader\".\n\nSince the decision to delay standardizing top-level `await` it has come up in a handful of committee discussions, primarily to ensure that it would remain possible in the language.\n\nIn May 2018, this proposal reached Stage 2 in TC39's process, with many design decisions (in particular, whether to block \"sibling\" execution) left open to be discussed during Stage 2.\n\n## Specification\n\n* [Ecmarkup source](https://github.com/tc39/proposal-top-level-await/blob/HEAD/spec.html)\n* [HTML version](https://tc39.github.io/proposal-top-level-await/)\n\n## Implementations\n\n* [V8 v8.9](https://v8.dev/blog/v8-release-89)\n* SpiderMonkey via [`javascript.options.experimental.top_level_await`](https://bugzilla.mozilla.org/show_bug.cgi?id=1519100) flag\n* [JavaScriptCore](https://bugs.webkit.org/show_bug.cgi?id=202484)\n* [webpack 5.0.0](https://webpack.js.org/blog/2020-10-10-webpack-5-release/)\n\n## References\n\n* https://github.com/bmeck/top-level-await-talking/\n* https://gist.github.com/Rich-Harris/0b6f317657f5167663b493c722647221\n\n[defer]: https://jakearchibald.com/2017/es-modules-in-browsers/#defer-by-default\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftc39%2Fproposal-top-level-await","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftc39%2Fproposal-top-level-await","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftc39%2Fproposal-top-level-await/lists"}