最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery done is not executed a 2nd time when an error is happening in the first done - Stack Overflow

programmeradmin1浏览0评论

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>
发布评论

评论列表(0)

  1. 暂无评论