2016-12-01 59 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

相關問題