AJAX and Deferred
jQuery AJAX methods ($.get, $.post, $.getJSON, $.ajax) all return jqXHR objects, which implement the Deferred interface.
jQuery AJAX methods ($.get, $.post, $.getJSON, $.ajax) all return jqXHR objects, which implement the Deferred interface.
// jqXHR is a Deferred
const req = $.getJSON("/api/data");
req.done(function(data) { renderData(data); });
req.fail(function(xhr) { console.error(xhr.statusText); });
req.always(function() { hideSpinner(); });
// You can also abort
// req.abort();
Storing the jqXHR lets you abort in-flight requests — useful for cancellable search.