📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials jQuery in Practice Fetch with jQuery Deferred

Fetch with jQuery Deferred

6 min read
Chain .done(), .fail(), and .always() on AJAX calls for clean async code. Use $.when(req1, req2) to wait for multiple parallel requests to complete before running a combined callback function.

Deferred and Promises

$.ajax({ url: '/api/users' })
  .done(function(data)  { console.log('ok', data); })
  .fail(function(xhr)   { console.error('failed'); })
  .always(function()    { console.log('done'); });

// Chain multiple requests
$.when(
  $.get('/api/users'),
  $.get('/api/products')
).done(function(users, products) {
  // both done
});