为jQuery延迟对象提供默认的"fail"方法

jd.*_*jd. 6 javascript jquery jquery-deferred

我正在使用jQuery编写Javascript API客户端.我的顶级请求方法如下所示:

function request(method, uri, params, proxies) {
  var deferred = $.Deferred();
  $.ajax({
    data: method == 'GET' ? params : JSON.stringify(params),
    contentType: 'application/json',
    dataType: 'json',
    url: api.root + uri,
    type: method,
    xhrFields: {
      withCredentials: true
    }
  }).done(function(body) {
    deferred.resolveWith(this, [body.data]);
  }).fail(function(xhr) {
    deferred.rejectWith(this, [xhr]);
  });

  return deferred.promise();
},
Run Code Online (Sandbox Code Playgroud)

如何fail为返回的延迟提供默认处理程序?也就是说,如果deferred没有附加到其fail条件的其他处理程序,则调用默认处理程序.

我想这样做是为了在我的应用程序中进行全局异常处理,除了具有特定处理的部分(并且将fail在延迟上定义自己的处理程序).

jfr*_*d00 3

因此,截至 2016 年,在 API 中使用 jQuery ajax 最简洁的方法是返回一个 Promise。但是,您无法确定调用者是否已将错误处理程序附加到 Promise。

因此,我建议您只需向函数添加一个新参数,告诉函数不要应用默认错误处理,因为调用者将负责错误处理。而且,我建议您通过仅使用已$.ajax()返回的现有承诺而不是创建自己的延迟来避免承诺反模式:

function request(method, uri, params, proxies, skipDefaultErrorHandling){
    // default error handling will be used if nothing is passed
    // for skipDefaultErrorHandling
    var p = $.ajax({
        data: method=='GET'?params:JSON.stringify(params),
        contentType: 'application/json',
        dataType: 'json',
        url:  api.root + uri,
        type: method,
        xhrFields: {
            withCredentials: true
        }
    });
    if (!skipDefaultErrorHandling) {
       // apply default error handling
       p = p.then(null, function(jqXHR, textStatus, errorThrown) {
           // put here whatever you want the default error handling to be
           // then return the rejection with the various error parameters available
           return $.Deferred().reject([jqXHR, textStatus, errorThrown]);
       });
    }

    return p;
};
Run Code Online (Sandbox Code Playgroud)

然后,调用者只需决定是否应用自己的错误处理:

request(...).then(function(data) {
    // success code here
});
Run Code Online (Sandbox Code Playgroud)

failHandler或者,您可以使用传入的非承诺回调,并且默认错误处理会查看是否failHandler传入了该回调。这是承诺和回调的混合体,不是我通常会选择构建的东西,但由于你的问题要求的是承诺不支持的东西,这是实现这一目标的方法之一:

function request(method, uri, params, proxies, failHandler){
    // default error handling will be used if nothing is passed
    // for skipDefaultErrorHandling
    var p = $.ajax({
        data: method=='GET'?params:JSON.stringify(params),
        contentType: 'application/json',
        dataType: 'json',
        url:  api.root + uri,
        type: method,
        xhrFields: {
            withCredentials: true
        }
    });
    // apply default error handling
    p = p.then(null, function(jqXHR, textStatus, errorThrown) {
       if (failHandler) {
           // call passed in error handling
           failHandler.apply(this, arguments);
       } else {
           // do your default error handling here
       }
       // then keep the promise rejected so the caller doesn't think it
       // succeeded when it actually failed
       return $.Deferred().reject([jqXHR, textStatus, errorThrown]);
    });

    return p;
};
Run Code Online (Sandbox Code Playgroud)