In jQuery (unlike some other libs), converting a promise from an “allowed” state to a “rejected” state is a little verbose, requiring the explicit creation and rejection of a new Pending one.
function foo() { return doAJAXRequest().then(function (data, textStatus, jqXHR) { if (data.responseText == "YES") { return doOtherAJAXRequest(data); } else { return $.Deferred().reject(jqXHR, data, 'Not YES').promise(); } }); )
Here the promise is returned if data.responseText !== "YES" imitates a (partially) failed ajax request, but still allows data to be transmitted. This is (probably) important for the downstream .fail() handler, which should handle both real ajax failures and the underserved success conditions, not knowing which one happened until it reads errorThrown .
foo().fail(function(jqXHR, textStatus, errorThrown) { if(errorThrown == 'Not YES') {
It is usually easier to pass data this way, rather than retrying it from the jqXHR object. This is especially true for JSON-encoded data that would otherwise need to be decoded a second time in the fault handler.
Beetroot-Beetroot Jul 24. '13 at 17:04 on 2013-07-24 17:04
source share