What is a jQuery Deferred, and how does it relate to Promises?
Quick Answer
A jQuery.Deferred is jQuery's promise implementation — you resolve()/reject() it and consumers attach .done()/.fail()/.then(). $.ajax() returns one. $.when(a, b) is jQuery's Promise.all. jQuery 3 made Deferreds Promises/A+ compliant so they interoperate with native promises and async/await.
A jQuery Deferred is jQuery's own version of a promise, from before promises were standardised. You create one, resolve or reject it, and callers hook on with .done, .fail or .then. $.ajax returns a Deferred, and $.when is the equivalent of Promise.all. jQuery 3 made them compliant with the Promises/A+ spec so they play nicely with native promises and you can even await them. In new code I'd just use native promises and fetch.
- Pre-jQuery 3, Deferred
.thendidn't behave to spec (swallowed exceptions, wrong resolution semantics) — jQuery 3 fixed this. - In new code, use native
Promise/fetch; Deferreds only matter when maintaining older jQuery code. - You can
awaita jQuery Deferred in modern browsers because it's thenable.