Difference Between jQuery always, done, fail and then

Written by: Idika Destiny

Reading time:
Published 1 Year Ago On Wednesday, August 9, 2023
321 Views



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.

The Deferred object is chainable, similar to the way a jQuery object is chainable, but it has its own methods. After creating a Deferred object, you can use any of the methods below by either chaining directly from the object creation or saving the object in a variable and invoking one or more methods on that variable.

Some Examples:

  1. Always – This method is called when the deferred object gets resolved or rejected
  2. Done – This method is only called when the deferred object is gets resolved or succeded
  3. Fail – This method is called when the deferred object is rejected or experienced an error
  4. Then – a nice shorthand that allows you to pass in a done and then optionally a fail and a progress

Always

$.get( "www.yoursite.com/api" ).always(function() {
  console.log('this will run whether the $.get fails or succeeds');
});

Done

$.get( "www.yoursite.com/api" ).done(function() {
  console.log('this will run only when the $.get succeeds');
});

Fail

$.get( "www.yoursite.com/api" ).fail(function() {
  console.log('this will run if the $.get fails');
});

Then

$.get( "www.yoursite.com/api" ).then(
    function() {
      console.log('this run if the $.get succeeds');
    }, function() {
        console.log('this run if the $.get fails');
    }, function() {
        console.log('this run if the deferred generates a progress update');
    }
);



The need for a top business owner or organization to have a professional, scalable, Fast, Optimized,Efficient, Very Secured web application (website) can never be over emphasized.
However, With this great tool (Web Application) Business Owners will definitely and Undoubtedly solidify their online presence, improve their Search Engine ranking, eliminate the likelihood of Missing out on search engine queries / results by prospective clients whom may search for a business like theirs on search engines like Bing and google, stay toe to toe with Compititors who already have a web application etc.
Read Now Top 15 Reasosns why you need a website for your Business
You don’t need to do all of these alone, We got you covered!! Contact us now your satisfaction is always our priority. price definitely won't be a problem.

Thanks for reading



What is Flash Storage | Different Between Flash Storage and Hard Disk Storage

The Importance of Emotional Intelligence in Fostering Connection and Belonging in Remote-First Companies