SyntaxStudy
Sign Up
jQuery $.when() for Parallel Requests
jQuery Intermediate 4 min read

$.when() for Parallel Requests

$.when()

$.when() waits for multiple Deferreds (or Promises) to complete. Its done callback fires when all succeed; fail fires when any fails.

Example
$.when(
  $.getJSON("/api/users"),
  $.getJSON("/api/products"),
  $.getJSON("/api/orders")
).done(function(users, products, orders) {
  // All three completed
  renderDashboard(users[0], products[0], orders[0]);
}).fail(function() {
  console.error("One or more requests failed");
});
Pro Tip

Each argument to the done callback is the response array from the corresponding request.