I'm having this problem in ES6 in a Babel Environment:
// A.js
class A {
}
export default new A();
// B.js
import C from './C';
class B {
}
export default new B();
// C.js
import A from './A';
import B from './B';
class C {
constructor(A, B){
this.A = A;
this.B = B; // undefined
}
}
export default new C(A, B)
I import them like this:
// stores/index.js
import A from './A';
import B from './B';
import C from './C';
export {
A,
B,
C
}
And from my app entry point I do:
import * as stores from './stores';
I would (hoped) expected the order of execution to be A -> B ->C but in reality it is A-> C-> B.
This is due to module B importing C, so that the C module is evaluated before the B module. This creates a problem in the setup of C because in that case B would be undefined.
I've seen a similar question but I'm not sure the init function is the best solution here, it seems a bit hacky.
Q: What's the best practise about solving this kind of circular dependencies in ES6 that would possibly work in different environments (Babel, Rollup)?