question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

import()ed module is not executed until .then() or .catch() is called on it

See original GitHub issue
// globalHaCKS.js

window.document.stateOverride13 = 'YOLO';

renderLegacyApp();
// just_make_it_less_slow.js

import('./globalHaCKS.js'); // Doesn't work!

import('./globalHaCKS.js').then(); // Works :s

When using webpack’s built in support for the import() statement, the imported module is executed as soon as the promise is created and the chunk is downloaded. With this package, it seems that the imported module is executed only when .then() or .catch() is called on the promise (even though either way the chunk is downloaded as soon as the promise is created).

This seems like an unnecessary and confusing breaking change compared to the way import() works in webpack. Any chance of changing it so that it is not necessary to call .then() or .catch() on the promise?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ScriptedAlchemycommented, May 8, 2018

This is something that’s been documented in the caveats.

Check out the Babel plugin readMe

Pretty much, you are not using a normal import function which webpack provides. It’s transformed into our universalImport

While it is thennable, it’s not self evoking. “Then” in our context, is a function which executes “load”, which is a promise.

Give this a read: https://github.com/faceyspacey/babel-plugin-universal-import/blob/master/universalImport.js ——— To the discerning eye, you may be wondering if the return of import() is still thenable?? It is! However, if you don’t call .then on it, somewhere (perhaps in the components like react-universal-component that you pass it to), then it won’t perform the import. Since most of us are using modules, which we need to do something with in the then callback, that’s not a problem. But if you happen to be importing a module that does its own setup, such as attaches something to the window object, well then you just need to call .then() to trigger it. That’s a rare case these days, which is why we decided to go with the simplicity seen here. And yes, async await works too.

1reaction
faceyspaceycommented, May 8, 2018

Thanks @zackljackson …couldn’t have explained it better. Hope that helps @hedgepigdaniel

Read more comments on GitHub >

github_iconTop Results From Across the Web

loadable not working in SSR with webpack module federation
To capture it during runtime and not compile time, we can proxy import's thenable. When 'then' is called, we add the chunk to...
Read more >
Understanding the Event Loop, Callbacks, Promises, and ...
The event loop checks the queue for any pending messages and finds the anonymous function from setTimeout() , adds the function to the...
Read more >
Dynamic imports - The Modern JavaScript Tutorial
The import() expression​​ The import(module) expression loads the module and returns a promise that resolves into a module object that contains ...
Read more >
8. Errors and Exceptions — Python 3.11.1 documentation
Errors detected during execution are called exceptions and are not unconditionally fatal: you will soon learn how to handle them in Python programs....
Read more >
async function - JavaScript - MDN Web Docs - Mozilla
async function asyncCall() { ... The function's name. ... by suspending execution until the returned promise is fulfilled or rejected.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found