Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery deferred reject immediately

When using JQuery.Deferred is it OK to invoke reject() directly? Without having invoked a async function?

Perhaps I want some kind of test in the beginning of my async function. If the test fails I want to reject immediately. See the first if block below.

function doSomethingAsync() {

    //Test if the ajax call should be invoked
    var testFailed = true;

    var dfd = $.Deferred();

    //Check if test failed
    if (testFailed) {
        var asyncResult = {
            success: false,
            data: 'test failed'
        };

        //Is this OK usage of reject on the same thread?
        dfd.reject(asyncResult);

        return dfd.promise();
    }


    $.get('/api/testapi/get').done(function (data) {
        var asyncResult = {
            success: true,
            data: data
        };

        dfd.resolve(asyncResult);
    }).fail(function (err) {
        var asyncResult = {
            success: false,
            data: err
        };

        dfd.reject(asyncResult);
    });

    return dfd.promise();
}
like image 380
Niclas Avatar asked Feb 02 '14 10:02

Niclas


People also ask

What does Deferred Reject do?

The deferred. reject() method in jQuery is used to reject a Deferred object and call any failCallbacks with the given arguments.

What is Deferred () in jQuery?

Deferred() A factory function that returns a chainable utility object with methods to 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 .

How use jQuery Deferred and promise?

promise() will attach the methods onto it and then return this object rather than create a new one. This can be useful to attach the Promise behavior to an object that already exists. If you are creating a Deferred, keep a reference to the Deferred so that it can be resolved or rejected at some point.


1 Answers

When using JQuery.Deferred is it OK to invoke reject() directly? Without having invoked a async function?

Yes, it's totally OK to return an already rejected promise, and to reject deferreds immediately. You only might need to verify that your callbacks don't rely on asynchronous resolution, which jQuery does not guarantee (in contrast to A+ implementations).

Notice that in your code you should use then instead of manually resolving the deferred:

function doSomethingAsync() {

    var testFailed = /* Test if the ajax call should be invoked */;

    var dfd = testFailed 
          ? $.Deferred().reject('test failed')
          : $.get('/api/testapi/get');

    return dfd.then(function (data) {
        return {
            success: true,
            data: data
        };
    }, function (err) {
        return {
            success: false,
            data: err
        };
    });
}
like image 173
Bergi Avatar answered Sep 17 '22 21:09

Bergi