jQuery Deferred - 等待多个 AJAX 请求完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6538470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:01:22  来源:igfitidea点击:

jQuery Deferred - waiting for multiple AJAX requests to finish

jqueryjquery-deferred

提问by brittohalloran

I have a three layer deep chain of deferred ajax calls, and ideally they are going to kick the promise all the way up when the deepest layer finishes (makes me thing of Inception... "we need to go deeper!").

我有一个三层深的延迟 ajax 调用链,理想情况下,当最深的层完成时,它们将一直执行承诺(让我觉得 Inception ......“我们需要更深入!”)。

The problem is that I'm sending off many ajax requests (possibly hundreds) at once and need to defer until all of them are done. I can't rely on the last one being done last.

问题是我一次发送了许多 ajax 请求(可能是数百个),并且需要推迟到所有这些请求都完成。我不能指望最后一个完成。

function updateAllNotes() {
    return $.Deferred(function(dfd_uan) {
        getcount = 0;
        getreturn = 0;
        for (i = 0; i <= index.data.length - 1; i++) {
            getcount++;
            $.when(getNote(index.data[i].key)).done(function() {
                // getNote is another deferred
                getreturn++
            });
        };
        // need help here
        // when getreturn == getcount, dfd_uan.resolve()
    }).promise();
};

回答by brittohalloran

You can use .when(), and .apply()with multiple deferred. Extremely useful:

您可以使用.when(), 和.apply()多个延迟。非常有用:

function updateAllNotes() {
    var getarray = [],
        i, len;

    for (i = 0, len = data.length; i < len; i += 1) {
        getarray.push(getNote(data[i].key));
    };

    $.when.apply($, getarray).done(function() {
        // do things that need to wait until ALL gets are done
    });
}

回答by Mordhak

If you refer to jQuery.Whendoc, if one of your ajax call fails, failmaster callback will be called even if all following ajax call haven't finished yet. In this case you are not sure that all your calls are finished.

如果您参考jQuery.Whendoc,如果您的 ajax 调用之一失败,fail即使所有后续 ajax 调用尚未完成,也会调用主回调。在这种情况下,您不确定所有呼叫是否都已完成。

If you want to wait for all your calls, no matter what the result is, you must use another Deferred like this :

如果你想等待所有的调用,不管结果是什么,你必须使用另一个 Deferred 像这样:

$.when.apply($, $.map(data, function(i) {
    var dfd = $.Deferred();
    // you can add .done and .fail if you want to keep track of each results individualy
    getNote(i.key).always(function() { dfd.resolve(); });
    return dfd.promise();
});

回答by Travis Watson

Thanks for the answer brittohalloran. I'm also using Underscore, so I was able to apply your solution very cleanly with map, kinda like this:

感谢您的回答 brittohalloran。我也在使用 Underscore,所以我能够用 map 非常干净地应用你的解决方案,有点像这样:

$.when.apply($, _.map(data, function(i) {
    return getNote(i.key);
})).done(function() {
    alert('Be Happy');
});

Wicked useful.

邪恶有用。