Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery $.when() difference between .then() and .done()

Tags:

jquery

I am trying to understand the difference between

$.when(...).then(...)  $.when(...).done(...) 

in jQuery. As far as I understood both of them executes when objects inside when are finished loading. But what is the difference. Examples will be really appreciated.

like image 247
Salvador Dali Avatar asked Jan 10 '13 13:01

Salvador Dali


People also ask

When jQuery deferred?

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.

What is .done in jQuery?

done() method in jQuery is used to add handlers which are to be called when the deferred object is resolved. Parameters: Callbacks: This parameter specifies a function, or array of functions, which are called when the Deferred is resolved.

What is done in promise?

A few promise libraries have a . done() method whose main purpose is to catch and rethrow any errors that were not handled, so that they show up in the console (in browsers) or crash the process in Node.

What is then in Ajax?

The ajax then is a global event that triggered on the document to call handler function, which may be listening. The ajax then can be performed with the help of the then() function. The jQuery jqXHR. then() function is a built-in function in jQuery. The function is specified by the jqXHR.


1 Answers

.done() has only success callback.

.then() has both success and fail callbacks.

As of jQuery 1.8, the deferred.then() method returns a new promise that can filter the status and values of a deferred through a function, replacing the now-deprecated deferred.pipe() method.

The deferred.done() method accepts one or more arguments, all of which can be either a single function or an array of functions.

Since deferred.done() returns the deferred object, other methods of the deferred object can be chained to this one, including additional .done() methods. When the Deferred is resolved, doneCallbacks are executed using the arguments provided to the resolve or resolveWith method call in the order they were added.

like image 58
Gurpreet Singh Avatar answered Nov 04 '22 02:11

Gurpreet Singh