← Back to jQuery Course | Chapter 12: Deferred & Promises | Lesson 5 of 5

Managing Multiple Async with when()

The when method waits for multiple Deferred or promise objects. It continues when all supplied operations succeed.
Syntax
javascript
$.when(promise1, promise2).done(function (result1, result2) {
  // all resolved
});

What is when()?

$.when() takes one or more Deferred objects or promises and returns a single combined promise that only settles once every one of them has settled. It's the tool for waiting on several independent asynchronous operations before continuing, instead of nesting their callbacks inside each other.

Example: What is when()?

javascript
<!DOCTYPE html>
<html>
<head>
  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <style>
    button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
    #resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
    #output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
  </style>
</head>
<body>
  <button id="runBtn">Run</button>
  <button id="resetBtn">Reset</button>
  <div id="output"></div>
  <script>

    $("#runBtn").click(function() {
      const a = $.Deferred();
      const b = $.Deferred();
      $.when(a, b).done(function() { $("#output").text("Both settled"); });
      a.resolve();
      b.resolve();
    });

    $("#resetBtn").click(function() {
      $("#output").empty();
    });
  </script>
</body>
</html>

when with AJAX

when() is especially useful for combining multiple asynchronous requests — say, three separate AJAX calls needed to build one page — so the final callback only runs after every request has come back successfully.

Example: when with AJAX

javascript
<!DOCTYPE html>
<html>
<head>
  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <style>
    button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
    #resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
    #output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
  </style>
</head>
<body>
  <button id="runBtn">Run</button>
  <button id="resetBtn">Reset</button>
  <div id="output"></div>
  <script>
    $(document).ajaxError(function(event, xhr, settings) {
      $("#output").append($("<div>").text("Request failed: " + settings.url));
    });

    $("#runBtn").click(function() {
      $("#output").empty();
      $("#output").append($("<div>").text("Sending request..."));
      $.when($.get("data.php"), $.get("data.php")).done(function(r1, r2) {
        $("#output").text("Both requests finished");
      });
    });

    $("#resetBtn").click(function() {
      jQuery(document).off();
      $("#output").empty();
    });
  </script>
</body>
</html>

Handle Failure

If any one of the combined operations fails, the fail() callback on the combined promise runs instead of done(), which is useful when the overall task genuinely requires every one of the individual pieces to succeed. A single failure is enough to mark the whole combined operation as failed.

Example: Handle Failure

javascript
<!DOCTYPE html>
<html>
<head>
  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <style>
    button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
    #resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
    #output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
  </style>
</head>
<body>
  <button id="runBtn">Run</button>
  <button id="resetBtn">Reset</button>
  <div id="output"></div>
  <script>

    $("#runBtn").click(function() {
      const a = $.Deferred();
      const b = $.Deferred();
      $.when(a, b).fail(function() { $("#output").text("At least one failed"); });
      a.resolve();
      b.reject();
    });

    $("#resetBtn").click(function() {
      $("#output").empty();
    });
  </script>
</body>
</html>

Use Different Delays

The individual operations passed to when() can take different amounts of time to complete, and when() will wait for the slowest one before resolving — it settles only once every operation it's tracking has finished. This makes it reliable even when the underlying tasks vary widely in duration.

Example: Use Different Delays

javascript
<!DOCTYPE html>
<html>
<head>
  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <style>
    button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
    #resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
    #output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
  </style>
</head>
<body>
  <button id="runBtn">Run</button>
  <button id="resetBtn">Reset</button>
  <div id="output"></div>
  <script>

    $("#runBtn").click(function() {
      const fast = $.Deferred();
      const slow = $.Deferred();
      setTimeout(function() { fast.resolve(); }, 50);
      setTimeout(function() { slow.resolve(); }, 300);
      $.when(fast, slow).done(function() { $("#output").text("Both finished, waited for slowest"); });
    });

    $("#resetBtn").click(function() {
      $("#output").empty();
    });
  </script>
</body>
</html>

Small when() Project

when() is especially valuable on pages that need several independent pieces of data — like a user profile, their recent orders, and site settings — before they can render, letting you wait for all three at once instead of chaining them unnecessarily.

Example: Small when() Project

javascript
<!DOCTYPE html>
<html>
<head>
  <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  <style>
    button { padding:6px 14px; margin:4px; background:#2c6aa0; color:white; border:none; border-radius:4px; cursor:pointer; }
    #resetBtn { background:white; color:#2c6aa0; border:1px solid #2c6aa0; }
    #output { margin-top:10px; padding:8px; border:1px solid #ccc; border-radius:4px; background:#f7f7f7; font-family:monospace; min-height:24px; }
  </style>
</head>
<body>
  <button id="runBtn">Run</button>
  <button id="resetBtn">Reset</button>
  <div id="output"></div>
  <script>
    $(document).ajaxError(function(event, xhr, settings) {
      $("#output").append($("<div>").text("Request failed: " + settings.url));
    });

    $("#runBtn").click(function() {
      $("#output").empty();
      $("#output").append($("<div>").text("Sending request..."));
      $.when($.get("data.php"), $.get("data.php"), $.get("data.php")).done(function() {
        $("#output").text("Profile, orders, and settings all loaded");
      });
    });

    $("#resetBtn").click(function() {
      jQuery(document).off();
      $("#output").empty();
    });
  </script>
</body>
</html>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Passing the results of calling functions instead of the promises, or a value with no promise, when $.when() needs Deferred objects such as $.when($.get(...), $.get(...)).
  2. Expecting done() to receive plain values for AJAX calls, when each argument is an array like [data, status, jqXHR].
  3. Assuming fail() waits for all the tasks, when it fires as soon as one fails and does not wait for the others.
Chapter Summary
  • Deferred objects represent work that finishes later, with states such as pending, resolved, and rejected.
  • You create and resolve deferreds and chain steps with promise pipeline methods.
  • when() manages multiple asynchronous tasks together.
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.