I am having trouble with some old code I have to maintain and wonder how to solve this the best way. Lets say I have multiple modules loaded in the page where I do not have control. All these modules are having an init function where they wait for some jQuery deferred objects/promises.
var d1 = jQuery.Deferred();
var d2 = jQuery.Deferred();
var d3 = jQuery.Deferred();
var p1 = d1.promise();
var p2 = d2.promise();
var p3 = d3.promise();
d1.resolve();
d2.resolve();
//I can not control this
function init1() {
jQuery.when(p3).done(function (v3) {
console.log("in init1 done");
//somewhere in the code is happening an error..
throw new Error("error happening here");
});
}
//this is my "init"
function init2() {
jQuery.when(p3).done(function (v3) {
console.log("in init2 done"); //this is never executed
});
}
try {
//when p1 is loaded, init1 is called.
//this is a module where I do not have control
jQuery.when(p1).done(function () {
init1();
});
}
catch {}
try {
//when the p2 file is loaded the init2 function is called
//this is my module
jQuery.when(p2).done(function () {
init2();
});
}
catch {}
//this is resolved at some later point
d3.resolve();
<script src=".11.3/jquery.min.js"></script>