11

test.html

<script type="module" src="./B.js"></script>

A.js

import {bar} from './B.js';
import {test} from './B.js';

export function foo() {
    bar();
}

test();
console.log("IN A");

B.js

import {foo} from './A.js';

export function bar() {
    if (Math.random()) {
        foo();
    }
}

export function test() {
    console.log("test");
}

console.log("IN B");

The output is:

B.js:30 test
A.js:24 IN A
B.js:33 IN B

Why is "In B" the last line of code that gets executed? Also, if in test.html, src="./A.js", "IN B" is the first line that gets executed.

Hyrial
  • 1,434
  • 1
  • 10
  • 18
  • 1
    How does that work with circular imports though? – Hyrial Jun 22 '18 at 18:14
  • 1
    You know what, I think this question may be more related. This answer goes into a ton of detail about circular dependencies: [How to fix this ES6 module circular dependency?](https://stackoverflow.com/a/42704874/691711) – zero298 Jun 22 '18 at 18:23

0 Answers0