【问题标题】:jQuery $.when does not wait for deferred chain to complete ajax callsjQuery $.when 不等待延迟链完成 ajax 调用
【发布时间】:2017-10-27 09:53:08
【问题描述】:

这里我尝试顺序加载几个 JS 文件。问题是$.when 不等待链完成。但最后一个 $.Deferred.then 按预期工作。我应该怎么做才能使$.when 工作?

var urls = [
    "https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js",
    "https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.2/photoswipe.js",
    "https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.2/photoswipe-ui-default.js"
];
var xxx = $.Deferred();
xxx.then(function() {
        options = {
            dataType: "script",
            url: urls[0],
            cache: false
        };
        return $.ajax(options).promise();
    })
    .then(function() {
        options = {
            dataType: "script",
            url: urls[1],
            cache: false
        };
        return $.ajax(options).promise();
    })
    .then(function() {
        options = {
            dataType: "script",
            url: urls[2],
            cache: false
        };
        return $.ajax(options).promise();
    }).then(function() {
        $("body").append("This is correct. All files are loaded.<br>");
    });

$.when(xxx).done(function() {
    $("body").append("This is wrong. JS files are not loaded yet.<br>");
});
xxx.resolve();
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt;

小提琴 https://jsfiddle.net/ergec/fmrj6qgp/

【问题讨论】:

  • 如果它按预期使用$.Deferred.then,那么为什么要使用$.when?
  • 完全没有理由在这里使用$.when() 或您的$.Deferred()。以$.ajax(...).then() 开始链。您的最后一个 .then() 处理程序应该可以很好地知道整个链何时完成。当 return $.ajax(...) 工作正常时,也没有理由使用 return $.ajax().promise()。

标签: jquery jquery-deferred


【解决方案1】:

您的方法不起作用,因为您从同一个 Promise 中创建了两个分支。你相当于:

var xxx = $.Deferred();
xxx.then(...).then(...);     // promise chain 1
xxx.then(...)                // promise chain 2
xxx.resolve();               // start the race between the two promise chains

这只是两个没有排序或协调的独立 Promise 链之间的竞赛。无论哪一个执行时间较短,都会先完成。它们之间的协调为零。

请参阅Chaining vs. Branching 和p.then().then() vs. p.then(); p.then() 了解更多说明。

而且,您对$.when(xxx).done() 的使用是不必要的,根本不是问题的一部分。你会得到与xxx.done() 相同的结果。


你可以通过只使用一个承诺链来修复它:

p.then(...).then(...).then(...)

然后,一切都得到了适当的排序和协调。

var urls = [
    "https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js",
    "https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.2/photoswipe.js",
    "https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.2/photoswipe-ui-default.js"
];
let options = {
  dataType: "script",
  url: urls[0],
  cache: false
};
$.ajax(options).then(function() {
    let options = {
      dataType: "script",
      url: urls[1],
      cache: false
    };
    return $.ajax(options);
}).then(function() {
    let options = {
      dataType: "script",
      url: urls[2],
      cache: false
    };
    return $.ajax(options).then(function() {
          $("body").append("Last script loaded<br>");
    });
}).then(function() {
      $("body").append("This is correct. All files are loaded.<br>");
});

工作示例:https://jsfiddle.net/jfriend00/tLp68a3q/


或者,如果您仍然想使用 var xxx = $.Deferred() 初始化您的链,以便稍后使用 xxx.resolve() 启动它,那么您也可以这样做,但您不能使用 xxx.then() 或 $.when(xxx),因为那样是一个完全独立的分支,根本不链接到另一个 Promise 链。

以$.Deferred()开头的工作示例:https://jsfiddle.net/jfriend00/y5n3hb99/

【讨论】:

    猜你喜欢
    • 2013-10-14
    • 2014-02-19
    • 1970-01-01
    • 2017-07-26
    • 2012-05-04
    • 1970-01-01
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多