I’m trying to understand $.when, and I can see that it can be useful when you want to wait for multiple deferreds before proceeding. However, I’m not sure I understand what the use-case is for using $.when with one deferred. To illustrate:
var deferred = $.Deferred();
// Is this ever useful?
$.when(deferred).then(...)
// Or can I always do this?
deferred.then(...)
From the
$.when[docs] documentation:So
$.when(deferred).then(...)is the same asdeferred.promise().then(...).The promise object is just a limited interface to the deferred object. It allows to add callbacks, but not to change the state of the Deferred (resolve, reject it).
So conclusively, there is basically no difference between using
$.whenand calling.thendirectly on the deferred object.I don’t think it makes sense to pass a single deferred object explicitly to
$.when, since you don’t get any advantage. However, there might be situations where you have an unknown number of deferred objects, which means it could also be only one.