What makes $ .if for one $ .Deferred?

I am trying to understand $.when , and I see that this can be useful when you want to wait a few pending until the beginning. However, I'm not sure I understand what is used to use $.when with one pending. To illustrate:

 var deferred = $.Deferred(); // Is this ever useful? $.when(deferred).then(...) // Or can I always do this? deferred.then(...) 
+8
jquery jquery-deferred
source share
2 answers

From the $.when [docs] documentation:

If one Deferred is passed for jQuery.when , its method returns a Promise object (a subset of the Deferred methods).

So $.when(deferred).then(...) same as deferred.promise().then(...) .

A promise object is a limited interface to a deferred object. It allows you to add callbacks, but not change the status of deferred (allow, reject).

So finally there is no fundamental difference between using $.when and calling .then directly on the deferred object.

I don't think it makes sense to pass one pending object explicitly to $.when , since you are not getting any advantage. However, there may be situations where you have an unknown number of pending objects, which means that it can also be only one.

+6
source share

You can always do .then , .then is nothing more than a shortcut when you need to use both .done and .fail

0
source share

All Articles