Why is there a spec for sync and async modules?

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 become magically synchronous if I write it in the CommonJS format, or how it becomes 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 ?


This is a good question. It's a subject that caused a lot of heated discussion in the Node community. 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 complex code that uses a lot of modules much more complicated.

    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 complex 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 communicate 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.

    链接地址: http://www.djcxy.com/p/55394.html

    上一篇: 网站文件夹布局

    下一篇: 为什么有同步和异步模块的规格?