tonitone120
tonitone120

Reputation: 2290

How can I tell which module will execute first?

ModuleA imports from ModuleB. And ModuleB imports from ModuleA. Both modules are loaded from the same folder into one .html file.

What is the rule that determines which one gets executed first here?

The module below the other one (in source-order) actually seems to get executed first. This seems strange considering I recall reading somewhere that the order modules are executed in is (partly) determined by source-order.


  <script type="module" src="../js/moduleB.js"></script>
  <script type="module" src="../js/moduleA.js"></script>

Upvotes: 0

Views: 177

Answers (1)

Pablo
Pablo

Reputation: 6058

Base on the information you are providing it makes sense that ModuleA is been executed first. Your second script tag importing ModuleA is actually redundant. Since you mentioned ModuleB imports ModuleA, ModuleA is executed first because all module imports are processed first. Assuming they are at the top of your script. This is more or less the order of execution.

  1. The script tag for ../js/moduleB.js is loaded.
  2. The imports for moduleB.js are loaded and proceed. This includes ModuleA.
  3. The rest of the code for ModuleB is executed.

Upvotes: 1

Related Questions