dynamic import() polyfill + question

Andrea Giammarchi andrea.giammarchi at gmail.com
Thu Apr 20 10:53:51 UTC 2017


P.S. the double `import('./js/a.js')` was a copy-pasta error, it should've
been `a` and `b`

```js
Promise.all([
  import('./js/a.js'),
  import('./js/b.js')
])
```

On Thu, Apr 20, 2017 at 11:51 AM, Andrea Giammarchi <
andrea.giammarchi at gmail.com> wrote:

> Even if unpolyfillable through simple `function import() {}` declaration,
> I've managed to create a polyfill/payground for the ESnext's dynamic
> import() [1]
>
> This also made me wonder if there's any plan to provide a way to
> asynchronously
> export modules that depends on those that use asynchronous import.
>
> Since AFAIK modules have no top-level await, the only pattern I can see
> right now
> to import something asynchronous is the following one:
>
> ```js
> // module ./js/c.js
> export default Promise.all([
>   import('./js/a.js'),
>   import('./js/a.js')
> ]).then([a, b] => {
>   const module = {a, b, c() {}};
>   return module;
> });
>
> // module that uses ./js/c.js
> import('./js/c.js').then(m => m.default).then(c => {
>   c.a(); c.b(); c.c();
> });
> ```
>
> However, above boilerplate doesn't seem ideal compared with something like
> the following:
>
> ```js
> // module ./js/c.js
> export default await Promise.all([
>   import('./js/a.js'),
>   import('./js/a.js')
> ]).then([a, b] => {
>   const module = {a, b, c() {}};
>   return module;
> });
>
> // module that uses ./js/c.js
> import * as c from './js/c.js';
> ```
>
> But again, AFAIK that's not possible.
>
> The clear advantage is that the module consumer wouldn't need to know, or
> care,
> if the loaded module depends on some dynamic, asynchronous, import,
> meaning modules can be updated and eventually moved to async transparently
> for any module consumer.
>
> As summary, is any solution worth exploring/improving/fixing/planning?
>
> Thank you.
> Best Regards
>
> [1] https://github.com/WebReflection/import.js#importjs
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170420/c22ad5f9/attachment.html>


More information about the es-discuss mailing list