by rco8786 · 10y ago · 0 comments
Is that right? `await` is non-blocking, no?
It's not difficult to imagine some code nested deep in some NPM module completely changing the load behaviour of your app without you knowing.
"Any module with a dependency on data that it needs to execute will have to wait for that data before it executes."
That's a good thing.
If your app is shipped as a single bundle this means your entry point would be unable to run anything while dependencies are awaiting if you use `import` declarations. If your app places runtime dependent imports into a dynamic import like using `import()` as a function, it would not be suspended while waiting.
> your entry point would be unable to run anything while dependencies are awaiting
is very different from the situation with synchronous I/O (in a single-threaded environment).
With sync I/O, not only is your entry point unable to do anything (which is what it chose to do, by using `import` instead of `import()`), your entire app is unable to do anything! So other entry points are also blocked.
Even worse, since sync I/O blocks the event loop, it prevents the user from interacting with your web page or Node app---e.g. scrolling, clicking links, pressing Ctrl+C to exit, and so on.
In other words, if you put
for (let i = 0; i < 10e8; i++)
// blah
in the body of a module, wouldn't that block dependent modules for the duration of the loop?Would top-level await be different?
edit The premise, as I understand it, is that the module itself is treated as the "currently-executing method" for the purpose of the `await`.