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.
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.
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.
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.
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.
.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-deprecateddeferred.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With