I'm struggling with loading JS files in sequential order, despite having looked at various other SO posts and documentation on async and defer. My code structure is as follows:
<script src="lang.js"></script> <!--dynamically loads either eng.js or fra.js-->
<script src="task1.js"></script> <!--all of the task*.js depend on eng.js/fra.js-->
<script src="task2.js"></script>
<script src="task3.js"></script>
<script src="task4.js"></script>
<script>
// inline JS; depends on all task*.js
</script>
The contents of lang.js are as follows:
let langScript = document.createElement("script")
// FR is a boolean defined earlier
langScript.setAttribute("src", FR ? "fra.js" : "eng.js");
langScript.setAttribute("async", "false");
let head = document.head;
head.insertBefore(langScript, head.firstElementChild);
By my understanding, these scripts should load and execute in the order eng.js/fra.js -> task*.js -> inline, but this doesn't seem to be the case (at least in Chrome and Firefox). How should I modify my code such that it executes in the correct order? (I would prefer not to use callbacks if possible as I don't want to change the individual JS files too much.)