I have a bunch of modules loaded asynchronously in javascript but they depend on each other. I was trying to write the easiest way to verify that: a) the module exists b) it has fully initialized
and do all this in a minimally written way without a copy pasted function on each module.
My solution is to have a loaded array on each module and then define the code as such:
const Module2= (() => {
const inited = {};
function init() {
try {
if (Module0.inited.init.true) Function.prototype();
if (Module1.inited.init.true) Function.prototype();
} catch (error) {
setTimeout(init, 250);
return;
}
...do stuff...
inited.init = {};
}
return {
loaded,
init,
...other funcs...
};
})();
Module2.init();
Does anyone know of a shorter and more elegant way? To clarify, it is not sufficient to just check whether the module just exists, it has to also have been initialized (basically its init() needs to have finished).