Deferred vs Promise
jQuery 3.x Deferreds are compatible with native Promises. They can be used with async/await since they implement the "thenable" interface.
jQuery 3.x Deferreds are compatible with native Promises. They can be used with async/await since they implement the "thenable" interface.
// jQuery Deferred with async/await
async function loadUser(id) {
const user = await $.getJSON("/api/users/" + id);
return user;
}
loadUser(1).then(user => console.log(user));
// Convert jQuery Deferred to native Promise
const nativePromise = Promise.resolve($.getJSON("/api/data"));
nativePromise.then(data => console.log(data));
In new code, prefer native Promises or async/await — use jQuery Deferreds only for jQuery API compatibility.