Description
This proposal adds a cool new feature (fetching/linking a module without executing immediately) but denies that feature to all modules which use top-level await (TLA).
This clashes with the current ESM model where modules that use TLA are treated just like any other modules, anywhere: import statements work normally for them and import()
is async already. ESM established a world where modules can safely use TLA without downsides.
This proposal breaks that model. It makes modules that use TLA worse than other modules because there is no way to fetch them while defering their execution.
This seems unnecessary, because there could just be an async version of defer
, which allows you to defer execution of an entire TLA module. Many consumers don't care about the described "problem" that exposing something becomes an async function; for them the async version of defer
would be the better version, because it would defer a larger part of the execution.
I propose:
import deferAsync * as myModule from "mypath"
Let's examine possible behaviors:
myModule
is a promise itself, and executed only when it is awaited- this seems a bit weird, because awaiting a promise can usually not trigger anything in JS
- all properties of
myModule
are promises; the module is executed only when one of them is accessed.- this seems natural, and is very similar to
defer
, wheremyModule
basically becomes a smart proxy for the module
- this seems natural, and is very similar to
To reiterate, deferAsync
would be a strictly better version of defer
for all consumers that are fine with getting promises.