Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

node.js - javascript - 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 ?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...