admin管理员组文章数量:1384131
I have just finished reading this article on Javascript modules. I can understand that CommonJS modules are synchronously loaded while AMD modules are asynchronously loaded.
What I don't understand is that how can I module bee magically synchronous if I write it in the CommonJS format, or how it bees magically async if I write it in the AMD format. I mean javascript does not have a define
or require
keyword even. All they are are specs not libraries.
I mean the behaviour of module loading is dependent on the module loader and not how the module is structured. And if that is the case why follow a coding pattern for different types of modules ?
Am I right in assuming that all libraries in the NodeJS world are synchronously loaded, regardless in what format they are written. And all modules in the browser space is asynchronously loaded.
If my above assumption is correct then why is there even a spec for UMD ? I mean if a script loads based on the environment it is present in then why make a spec for universal module loading ?
Can someone help me with this confusion ?
I have just finished reading this article on Javascript modules. I can understand that CommonJS modules are synchronously loaded while AMD modules are asynchronously loaded.
What I don't understand is that how can I module bee magically synchronous if I write it in the CommonJS format, or how it bees magically async if I write it in the AMD format. I mean javascript does not have a define
or require
keyword even. All they are are specs not libraries.
I mean the behaviour of module loading is dependent on the module loader and not how the module is structured. And if that is the case why follow a coding pattern for different types of modules ?
Am I right in assuming that all libraries in the NodeJS world are synchronously loaded, regardless in what format they are written. And all modules in the browser space is asynchronously loaded.
If my above assumption is correct then why is there even a spec for UMD ? I mean if a script loads based on the environment it is present in then why make a spec for universal module loading ?
Can someone help me with this confusion ?
Share Improve this question asked Nov 10, 2016 at 8:34 ng.newbieng.newbie 3,2706 gold badges32 silver badges71 bronze badges 3- how a "module" is loaded makes no change to the code - your assumption about the code inside the module is your stumbling block – Jaromanda X Commented Nov 10, 2016 at 8:35
- @JaromandaX Yes so why are there module specs. Could you please elaborate on what you mean ? – ng.newbie Commented Nov 10, 2016 at 8:43
- The module itself does not bee synchronous vs asynchronous, it's just what kind of loader interface they call into. The CJS format assumes a synchronous loader, which doesn't work well on browsers, so AMD format was created to allow dependency declaration with async body execution (a callback). AMD would work with a synchronous loader as well. – Bergi Commented Nov 10, 2016 at 10:43
2 Answers
Reset to default 6This is a good question. It's a subject that caused a lot of heated discussion in the Node munity. To have a good understanding of what it's all about you should read:
- Node.js, TC-39, and Modules by James M Snell from iBM
- ES6 Module Interoperability - Node.js Enhancement Proposals
- In Defense of .js - A Proposal for Node.js Modules by Dave Herman, Yehuda Katz and Caridy Patiño
- Discussion on the Pull Request #3 of node-eps (002: ES6 module interop)
Now, answering your question - Why is there a spec for sync and async modules? Because some usecases prefer the synchronous loading of modules, like the server-side modules in Node.js where you want to load everything you need before you start serving requests, and some usecases prefer asynchronous loading of modules, like in the browser when you don't want to block the rendering thread while you load the dependencies.
There is really no option for synchronous loading in the browser because it would make the browser not responsive.
You could argue that you might use asynchronous loading on the server but then you'd either have to return promises instead of modules by require()
or it could take callbacks. Either way it would make any plex code that uses a lot of modules much more plicated.
Another issue is with the caching and mutation of the already loaded modules. With synchronous module loading using require
you only load the
module once and any other calls to require
for the same module in the entire code base (for that process) return a cached response, which is the same object every time. Any part of the code can modify that object and it is available to any other part of the code. Some usecases that use that feature would be much more plex to implement. Additionally the order of loading and executing code would be harder to predict.
To sum up the answer to your question, there are arguments for both ways of loading modules and neither of those ways is a clear winner for every scenario. Both are needed and both have some specs to standardize their behavior.
Read the articles that I linked for more detailed understanding.
I mean the behaviour of module loading is dependent on the module loader and not how the module is structured. And if that is the case why follow a coding pattern for different types of modules ?
Whether the module is loaded synchronously or asynchronously depends on module loader, but your module must be able to use a module loader, thus must include interface to municate with the loader.
You can't load modules in node.js using amd define
function. You have to use require
.
If my above assumption is correct then why is there even a spec for UMD ? I mean if a script loads based on the environment it is present in then why make a spec for universal module loading ?
Script doesn't load based on the environment, it loads through loader interface. UMD is for libraries that people use both in browser and server. The library author doesn't have to create two versions of the library, one for browser and one for node because UMD
knows how to handle both.
本文标签: nodejsjavascriptWhy is there a spec for sync and async modulesStack Overflow
版权声明:本文标题:node.js - javascript - Why is there a spec for sync and async modules? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744512616a2609971.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论