2016-12-01 55 views
1

说我有一个延迟功能:

尝试1:

$.when(validatePerson(123), validatePerson(456)) 
.done(function(data) { }) 
.fail(function(data1, data2) { }); 

尝试2:

$.when(validatePerson(123), validatePerson(456)) 
.done(function(data) { }) 
.fail(function(data1) { }, 
     function(data2) { }); 

我想进行2个AJAX调用异步,但在失败,我想能够确定哪一个第一,第二或两个调用fai导致我可以向用户显示适当的错误。

例如

  • 验证人1(ID 123)失败
  • 验证人2(编号456)失败

但我似乎无法得到它的工作。 在尝试1中,data1参数仅包含其中一个结果,并且data2未定义。

在尝试2中我得到了用相同的参数调用两次的函数回调。

+0

使用jQuery是这不可能?这是否会在IE11中工作,因为我将Promises看成是ES6的东西? – user183872

回答

0

$.when()终止时,第一个承诺,你通过它拒绝。你无法得到所有被拒绝的承诺。您可以将其与其他代码一起使用来跟踪所有故障。

这里有一个$.settle()是等待所有承诺你传递它是解决或拒绝,并返回PromiseInspection对象的数组,从中可以知道哪些解决和拒绝,以及他们的解决值或拒绝的理由是:

(function() {  

    function isPromise(p) { 
     return p && (typeof p === "object" || typeof p === "function") && typeof p.then === "function"; 
    } 

    function wrapInPromise(p) { 
     if (!isPromise(p)) { 
      p = $.Deferred().resolve(p); 
     } 
     return p; 
    } 

    function PromiseInspection(fulfilled, val) { 
     return { 
      isFulfilled: function() { 
       return fulfilled; 
      }, isRejected: function() { 
       return !fulfilled; 
      }, isPending: function() { 
       // PromiseInspection objects created here are never pending 
       return false; 
      }, value: function() { 
       if (!fulfilled) { 
        throw new Error("Can't call .value() on a promise that is not fulfilled"); 
       } 
       return val; 
      }, reason: function() { 
       if (fulfilled) { 
        throw new Error("Can't call .reason() on a promise that is fulfilled"); 
       } 
       return val; 
      } 
     }; 
    } 

    // pass either multiple promises as separate arguments or an array of promises 
    $.settle = function(p1) { 
     var args; 
     if (Array.isArray(p1)) { 
       args = p1; 
     } else { 
      args = Array.prototype.slice.call(arguments); 
     } 

     return $.when.apply($, args.map(function(p) { 
      // make sure p is a promise (it could be just a value) 
      p = wrapInPromise(p); 
      // Now we know for sure that p is a promise 
      // Make sure that the returned promise here is always resolved with a PromiseInspection object, never rejected 
      return p.then(function(val) { 
       return new PromiseInspection(true, val); 
      }, function(reason) { 
       // convert rejected promise into resolved promise by returning a resolved promised 
       // One could just return the promiseInspection object directly if jQuery was 
       // Promise spec compliant, but jQuery 1.x and 2.x are not so we have to take this extra step 
       return wrapInPromise(new PromiseInspection(false, reason)); 
      }); 
     })).then(function() { 
       // return an array of results which is just more convenient to work with 
       // than the separate arguments that $.when() would normally return 
      return Array.prototype.slice.call(arguments); 
     }); 
    } 

    // simpler version that just converts any failed promises 
    // to a resolved value of what is passed in, so the caller can just skip 
    // any of those values in the returned values array 
    // Typically, the caller would pass in null or 0 or an empty object 
    $.settleVal = function(errorVal, p1) { 
     var args; 
     if (Array.isArray(p1)) { 
       args = p1; 
     } else { 
      args = Array.prototype.slice.call(arguments, 1); 
     } 
     return $.when.apply($, args.map(function(p) { 
      p = wrapInPromise(p); 
      return p.then(null, function(err) { 
       return wrapInPromise(errorVal); 
      }); 
     })); 
    } 
})(); 

用法:

$.settle(arrayOfPromises).then(function(results) { 
    // results is an array of PromiseInspection objects and you can 
    // tell which are resolved and which are rejected 
    results.forEach(function(item, index) { 
     if (item.isFulfilled()) { 
      console.log("promise #" + index + " is fulfilled with value: ", item.value();); 
     } else { 
      console.log("promise #" + index + " is rejected with reason: ", item.reason();); 
     } 
    }); 
}); 
+0

谢谢,但这只是2 Ajax回叫很多代码!另外我想知道fail()函数中回调数组的目的是什么? – user183872

+0

@ user183872 - '$ .when()'一旦第一次承诺失败就拒绝,所以你没有机会看到其他承诺发生了什么。它只是不这样工作。您可以将多个回调函数传递给'.fail()',它只是针对同一事件的多个回调函数。它会一个接一个地打电话给他们,所有的都有相同的信息。 – jfriend00

+0

@ user183872 - 你应该把它看作是jQuery扩展的工具。你可以将它添加到你的项目中,这样你就可以在任何时候使用'$ .settle()'来查看所有promise的所有结果,而不管是否解析或拒绝。然后,只需使用它来代替'$ .when()'时,这就是你想要的。这不是每次你想使用它时输出的东西。 – jfriend00

相关问题