Skip to content

Instantly share code, notes, and snippets.

@MylesBorins
Last active June 5, 2019 08:41
Show Gist options
  • Save MylesBorins/ae97abab4a4144411c12240bb09dc7dd to your computer and use it in GitHub Desktop.
Save MylesBorins/ae97abab4a4144411c12240bb09dc7dd to your computer and use it in GitHub Desktop.
Why not import await?

why not import await instead of top-level await?

Some historical discussion

Concerns raised in initial import await gist

  • Asynchronicity of imported module is not visible
    • Imports of async modules can have potentially performance impact and should be visible to the developer
    • It's difficult to see which modules are affected by the top-level-await
    • Once this is visible, developer can break the async chain with import()
  • Microticks between imports may be unexpected
    • Developer don't know if modules are evaluated sync or async
    • Execution order is no longer guaranteed when modules add top-level-awaits

How the current specification works

  • No more microticks unless there is top-level await
    • If no top-level await in graph then it is executed synchronously
  • Optmizes async execution
    • Only the modules that include a Top-Level Await are run asyncronously
    • Subsquent sub graphs are run synchronously
    • No micro ticks for remaining graph

Concerns with import await

  • Viral Syntax
    • no way to introduce async module into graph without import await to root
    • use of import await defensively likely to happen
  • wasm modules will be async
    • will force introduction of wasm modules to be Semver-Major (and viral)
  • Removes ability for engine to optimize async execution
    • import await would force graph up to root to execute asynchronously
    • running import async modules synchronously would break developer contract

In Summary

  • It is my belief that import await is not a replacement for Top-Level Await
  • This does not mean that others, e.g. webpack, should not experiment with the API
  • this does not preclude import await from being presented to the committee as an additional proposal in the future
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment