Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throwing an Error in jQuery's Deferred object

I have an $.ajax promise and want to check whether my (syntactically valid) response contains an error, triggering the rejected status in that case.

I have worked with my own promise library which deals such tasks easily. I do not really like jQuery's Promise (cache) implementation with its Deferred object and may have overlooked something, because I seldom use it. I think the way to go is just using .then(), which seems to be rather complicated:

return $.ajax(...).then(function success(response) {
    var problem = hasError(response);
    if (problem) {
        var error = new $.Deferred;
        error.reject(problem);
        return error;
    } else
        return response;
});

This should return a promise which is rejected in case of network errors or problems with the response. But is returning a rejected deferred really the [only|best|shortest] way to go?

I also would appriciate help on how to deal with such "error-throwing response handlers" in the ajax options themselfes, I could not find good documentation about them.


Disclaimer: No, I cant change the server responses. The problem-detecting method is synchronous. I don't want to use other libraries, I'm particularly interested in the way jQuery solves this.

like image 791
Bergi Avatar asked Jun 01 '12 01:06

Bergi


People also ask

What is a Deferred object?

The Deferred object, introduced in jQuery 1.5, is a chainable utility object created by calling the jQuery. Deferred() method. It can register multiple callbacks into callback queues, invoke callback queues, and relay the success or failure state of any synchronous or asynchronous function.

What does Deferred resolve () return?

resolve( [args ] )Returns: Deferred. Description: Resolve a Deferred object and call any doneCallbacks with the given args .

Which among the given below jQuery objects when created can protect Deferred objects from altering States by other code?

The deferred. promise() method allows an asynchronous function to prevent other code from interfering with the progress or status of its internal request.

What is Deferred resolve?

deferred. resolve() - means request succeeded. deferred. reject() - request failed.


1 Answers

Now updated for jQuery 1.8+

The easiest way to tackle this is to run the response of $.ajax through .then to filter based on success or failure of the data.

$.ajax()
    .then(function (response) {
        return $.Deferred(function (deferred) {
            var problem = hasError(response);

            if (problem) {
                return deferred.reject(problem)
            }

            deferred.resolve(response);
        }).promise();
    });

You could then return this new promise to whatever calling code would consume this:

var request = function () {
    return $.ajax()
        .then(function (response) {
            return $.Deferred(function (deferred) {
                var problem = hasError(response);

                if (problem) {
                    return deferred.reject(problem)
                }

                deferred.resolve(response);
            }).promise();
        });
};

request()
    .done(function (response) {
        // handle success
    })
    .fail(function (problem) {
        // handle failure
    });
like image 128
Eli Avatar answered Sep 21 '22 15:09

Eli