← 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.

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>
  </head>
  <body>
    <script>
      const a = $.Deferred();
      const b = $.Deferred();
      $.when(a, b).done(function() { console.log("Both settled"); });
      a.resolve();
      b.resolve();
    </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>
  </head>
  <body>
    <script>
      $.when($.get("data.php"), $.get("data.php")).done(function(r1, r2) {
      console.log("Both requests finished");
      });
    </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>
  </head>
  <body>
    <script>
      const a = $.Deferred();
      const b = $.Deferred();
      $.when(a, b).fail(function() { console.log("At least one failed"); });
      a.resolve();
      b.reject();
    </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>
  </head>
  <body>
    <script>
      const fast = $.Deferred();
      const slow = $.Deferred();
      setTimeout(function() { fast.resolve(); }, 50);
      setTimeout(function() { slow.resolve(); }, 300);
      $.when(fast, slow).done(function() { console.log("Both finished, waited for slowest"); });
    </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>
  </head>
  <body>
    <script>
      $.when($.get("data.php"), $.get("data.php"), $.get("data.php")).done(function() {
      console.log("Profile, orders, and settings all loaded");
      });
    </script>
  </body>
</html>
🔒

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.