I return within an ajax request a custom html tag:
<eval-script src="script1.js"></eval-script>
<eval-script src="script2.js"></eval-script>
<eval-script src="script3.js"></eval-script>
Then I parse the response and store all script executions in an array (arrow functions):
const evalArray = [];
const getScript = function(src, dfd) {
$.getScript(src)
.done(function() {
dfd.resolve();
})
.fail(function() {
dfd.reject();
});
}
const evalScripts = document.querySelectorAll("eval-script");
for (const script of evalScripts) {
const src = script.getAttribute("src");
evalArray.push((dfd) => getScript(src, dfd));
};
After that I iterate through the array and try to wait for script to be loaded (non-deferred, synchronous):
evalArray.forEach((func) => {
const dfd = $.Deferred();
func(dfd);
while (dfd.state() === "pending") {
console.log("wait for resolve (" + dfd.state() + "): " + func);
};
return;
});
This is done because I want the scripts to execute in the submitted order and they could rely on the previous script. Also I want to execute some script after that that relies on the scripts being loaded.
Unfortunatly I can see $.getScript is requesting the script, and the scripts response headers are shown in browsers network tab, but there is no content and the promises done or fail are not executed.
The scripts can be opened by url in browser with no problem. Also I can execute "$.getScript('script1.js')" in console without any problem.
I do not want to use synchronous Ajax because that is deprecated option in jquery. Also I have no problem with promise setup, but getScript is not executing as expected.
Can someone please help?