Continue jQuery failure Delayed chain

I am making a series of consecutive AJAX calls in jQuery using the regular chaining method with Deferred. The first call returns a list of values, and subsequent calls are made with the data from the returned lists. After the first call that returns the list, subsequent calls can be made in any order, but they must be executed one at a time. So here is what I use:

$.when(callWebService()).then(
function (data) {
    var looper = $.Deferred().resolve(),
        myList = JSON.parse(data);
    for (var i in myList) {
        (function (i) {
            looper = looper.then(function () { // Success
                return callWebService();
            }, 
            function (jqXHR, textStatus, errorThrown) { // Failure
                if (checkIfContinuable(errorThrown) == true)
                    continueChain();
                else
                    failWithTerribleError();
            });
        })(i);
    }
});

It turns out that subsequent calls may fail from time to time, but I still want to make the rest of the calls. In my listing, what this little ingenious pseudo-code should do:

if (checkIfContinuable(errorThrown) == true)
    continueChain();
else
    failWithTerribleError();

continueChain? , , . .

+4
2

Promises/A + ,

promise.then(…, function(err) {
    if (checkIfContinuable(err))
        return valueToConinueWith;
    else
        throw new TerribleError(err);
})

, jQuery - Promises/A + ( ) - jQuery , , :

jDeferred.then(…, function(err) {
    if (checkIfContinuable(err))
        return $.Deferred().resolve(valueToConinueWith);
    else
        return $.Deferred().reject(new TerribleError(err));
})
+5

jQuery , Promises/A +, , , .catch .then. /, .

jQuery . A. Then (.catch ) . "", , .

, , Array.prototype.reduce().

function getWebServiceResults() {
    return callWebService().then(function(data) {
        var myList;

        // This is genuine Javascript try/catch, in case JSON.parse() throws.
        try {
            myList = JSON.parse(data);
        }
        catch (error) {
            return $.Deferred().reject(error).promise();//must return a promise because that what the caller expects, whatever happens.
        }

        //Now use `myList.reduce()` to build a promise chain from the array `myList` and the items it contains.
        var promise = myList.reduce(function(promise, item) {
            return promise.then(function(arr) {
                return callWebService(item).then(function(result) {
                    arr.push(result);
                    return arr;
                }, function(jqXHR, textStatus, errorThrown) {
                    if(checkIfContinuable(errorThrown)) {
                        return $.when(arr); // return a resolved jQuery promise to put promise chain back on the success path.
                    } else {
                        return new Error(textStatus);//Although the error state will be naturally propagated, it generally better to pass on a single js Error object rather than the three-part jqXHR, textStatus, errorThrown set.
                    }
                });
            });
        }, $.when([])) // starter promise for the reduction, resolved with an empty array

        // At this point, `promise` is a promise of an array of results.

        return promise.then(null, failWithTerribleError);
    });
}

:

  • function getWebServiceResults() {...}. ,
  • callWebService() item - myList.
  • , checkIfContinuable() . , errorThrown, jqXHR textStatus.
+1

Source: https://habr.com/ru/post/1612498/


All Articles