模块加载器负责加载模块。
我所知道的是模块加载器在浏览器中异步加载模块,而在 Node.js 中它是同步加载的。
我想确认这个信息是否正确。
最佳答案
ES6 模块加载器将是异步的,而 node.js 模块加载器则不是。
以下是模块加载器的一些关键方面:
Module code automatically runs in strict mode and there’s no way to opt-out of strict mode.
Variables created in the top level of a module are not automatically added to the shared global scope. They exist only within the top-level scope of the module.
The value of
this
in the top level of a module is undefined. Does not allow HTML-style comments within the code (a leftover feature from the early browser days).Modules must export anything that should be available to code outside of the module.
https://leanpub.com/understandinges6/read#leanpub-auto-modules
Modules, in general, solve several problems for developers. First, they allow the developer to separate code into smaller pieces, called modules. Second, they make it easy for developers to load (inject) those modules into other sections of code. Having modules injected like this helps keep project code uncoupled from the module (read: improved testability). And third, modules can load scripts asynchronously. This means that apps can begin loading faster, as they don’t require all scripts to be loaded prior to executing code.
http://chimera.labs.oreilly.com/books/1234000001623/ch03.html#_default_values
另一方面,因为 node.js
基于同步的 require
这意味着 node.js
不提供异步变体盒子外面。
当然有用于 Node 的异步模块加载器(async-require),但不支持 native (需要)。
关于javascript - ES6 模块是同步加载还是异步加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31807799/