Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Deferred advantages?

Tags:

jquery

 $.ajax("example.php").done(function ()
 {
     alert(...);
 }).fail(function ()
 {
     alert(...);
 }).always(function ()
 {
      alert(...);
 });

vs

  $.ajax("example.php",

      success: function ()
      {
          alert('');
      },
      error: function ()
      {
          alert('');
      },
      complete: function ()
      {
          alert('');
      }
  );

Sorry , I can't see any advantages

Can you please explain ?

Or maybe can you give me example of the former which cant be accomplished by the latter...?

like image 710
Royi Namir Avatar asked Nov 06 '11 20:11

Royi Namir


People also ask

What is the use of Deferred in jQuery?

Deferred() method in JQuery is a function which returns the utility object with methods which can register multiple callbacks to queues. It calls the callback queues, and relay the success or failure state of any synchronous or asynchronous function.

Can jQuery be Deferred?

The jQuery. Deferred method can be passed an optional function, which is called just before the method returns and is passed the new deferred object as both the this object and as the first argument to the function. The called function can attach callbacks using deferred. then() , for example.

What is jQuery Deferred and promise object?

The Deferred object can be employed when performing asynchronous operations, such as Ajax requests and animations. In jQuery, the Promise object is created from a Deferred object or a jQuery object. It possesses a subset of the methods of the Deferred object: always() , done() , fail() , state() , and then() .

What is a Deferred Javascript?

Deferred: The defer attribute tells the browser not to interfere with the HTML parsing and only to execute the script file once the HTML document has been fully parsed.


1 Answers

For starters, the second code block isn't even syntactically valid. I'd call that an advantage of the first :P


But in all seriousness:

It's just a different syntax for achieving the same end result. One immediate advantage is that you can wire up multiple functions for each outcome with deferred:

$.get("example.php").done(function ()
{
    alert("success 1");
}).done(function ()
{
    alert("success 2");
});

otherwise you'd have to do it like so:

function done1()
{
    alert('success 1');
}
function done2()
{
    alert('success 2');
}

$.ajax('example.php',
{
    success: function ()
    {
        done1.apply(this, arguments);
        done2.apply(this, arguments);
    }
});

Other advantages, quoted from How can jQuery deferred be used?

  • Deferreds are perfect for when the task may or may not operate asynchronously, and you want to abstract that condition out of the code.
  • ...Fetching data from multiple sources. In the example below, I'm fetching multiple, independent JSON schema objects used in an existing application for validation between a client and a REST server. In this case, I don't want the browser-side application to start loading data before it has all the schemas loaded. $.when.apply().then() is perfect for this.
  • A deferred can be used in place of a mutex. This is essentially the same as the multiple ajax usage scenarios.
like image 113
Matt Ball Avatar answered Sep 19 '22 23:09

Matt Ball