SyntaxStudy
Sign Up
jQuery AJAX Returns a Deferred
jQuery Intermediate 3 min read

AJAX Returns a Deferred

AJAX and Deferred

jQuery AJAX methods ($.get, $.post, $.getJSON, $.ajax) all return jqXHR objects, which implement the Deferred interface.

Example
// 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();
Pro Tip

Storing the jqXHR lets you abort in-flight requests — useful for cancellable search.