javascript 在 jQuery 的 Deferred 对象中抛出错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10843297/
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-10-26 11:13:34  来源:igfitidea点击:

Throwing an Error in jQuery's Deferred object

javascripterror-handlingjquery-deferredpromise

提问by Bergi

I have an $.ajaxpromise and want to check whether my (syntactically valid) response contains an error, triggering the rejected status in that case.

我有一个$.ajax承诺,想检查我的(语法上有效的)响应是否包含错误,在这种情况下触发拒绝状态。

I have worked with my own promise library which deals such tasks easily. I do not really like jQuery's Promise(cache) implementation with its Deferredobjectand may have overlooked something, because I seldom use it. I think the way to go is just using .then(), which seems to be rather complicated:

我使用过自己的 Promise 库,可以轻松处理此类任务。我不太喜欢 jQuery 的Promise( cache) 实现及其Deferred对象,并且可能忽略了一些东西,因为我很少使用它。我认为要走的路只是使用.then(),这似乎相当复杂:

return $.ajax(...).then(function success(response) {
    var problem = hasError(response);
    if (problem) {
        var error = new $.Deferred;
        error.reject(problem);
        return error;
    } else
        return response;
});

This should return a promise which is rejected in case of network errors or problems with the response. But is returning a rejected deferred really the [only|best|shortest] way to go?

这应该返回一个承诺,在网络错误或响应问题的情况下被拒绝。但是返回一个被拒绝的 deferred 真的是 [only|best|shortest] 方法吗?

I also would appriciate help on how to deal with such "error-throwing response handlers" in the ajax options themselfes, I could not find good documentation about them.

我也会在如何处理 ajax 选项本身中的此类“错误抛出响应处理程序”方面提供帮助,但我找不到关于它们的良好文档。



Disclaimer: No, I cant change the server responses. The problem-detecting method is synchronous. I don't want to use other libraries, I'm particularly interested in the way jQuery solves this.

免责声明:不,我无法更改服务器响应。问题检测方法是同步的。我不想使用其他库,我对 jQuery 解决这个问题的方式特别感兴趣。

回答by Eli

Now updated for jQuery 1.8+

现在为 jQuery 1.8+ 更新

The easiest way to tackle this is to run the response of $.ajaxthrough .thento filter based on success or failure of the data.

解决这个最简单的方法是运行的响应$.ajax,通过.then基于数据的成功或失败的过滤器。

$.ajax()
    .then(function (response) {
        return $.Deferred(function (deferred) {
            var problem = hasError(response);

            if (problem) {
                return deferred.reject(problem)
            }

            deferred.resolve(response);
        }).promise();
    });

You could then return this new promise to whatever calling code would consume this:

然后,您可以将这个新承诺返回给任何调用代码会使用它:

var request = function () {
    return $.ajax()
        .then(function (response) {
            return $.Deferred(function (deferred) {
                var problem = hasError(response);

                if (problem) {
                    return deferred.reject(problem)
                }

                deferred.resolve(response);
            }).promise();
        });
};

request()
    .done(function (response) {
        // handle success
    })
    .fail(function (problem) {
        // handle failure
    });