Error Handling and Recovery Using jQuery Deferred

I am using jQuery and I know that this problem is due to the fact that the jQuery.Deferred implementation is not Promises / A + compiant. I do not want to use other libraries to solve this problem.

From this point of view, is there a way to recover from the callback $.Deferred().fail()so that I return to the success chain? This is possible using a form with multiple callbacks then(), but so far I have not found a solution using.fail()

then

asyncThatWillFail().then(function () {
    // useless callback
}, function () {
    console.log("error");
    return $.Deferred().resolve();
}).then(function () {
    console.log("continuing on success chain");
});

fail (not working):

asyncThatWillFail().fail(function () {
    console.log("error");
    return $.Deferred().resolve();
}).then(function () {
    console.log("continuing on success chain");
});

In my case, I only need to check for an error, set a flag, and continue what I am doing. I just don't need a parallel success handler in the "then" example.

Here is jsFiddle to clarify what I mean.

+4
1

, .fail . .then:

null, .

then ,

asyncThatWillFail().then(null, function () {
    console.log("error");
    return $.Deferred().resolve();
}).then(function () {
    console.log("continuing on success chain");
});

jQuery, ES6 then, catch .then(null, …).

+3

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


All Articles