When a module
A is imported inside both module B and C, will the code inside A be executed 2 times in total? For example, if I invoke fetch()
within A, will fetch()
run twice?
Here's an illustration:
//file: A.js
var A = {
init: false,
count: 0
};
if(A.init == false) {
A.init = true;
A.value = fetch("https://...");
}
I always thought that module codes are only executed once for the entire lifespan of the webpage regardless of how many times they are imported.
Therefore, if I assign A.count = 1
inside module B, then the value of A.count
should also be 1 within module C.
However, as per "Import a module for its side effects only" on:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
I can actually execute the code within module A multiple times for each module (B, C, D) that utilizes it. How does this align with the previous understanding?