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

Creating and Resolving Deferred

Use $.Deferred to create a Deferred object. It starts in the pending state.

Create a Deferred

$.Deferred() constructs a new Deferred object representing a task that hasn't resolved or rejected yet. You typically create one at the start of a function that wraps some asynchronous work, like a timer or a manual request, so you can control exactly when it settles.

Example: Create a Deferred

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      const dfd = $.Deferred();
      console.log("Created, state:", dfd.state());
    </script>
  </body>
</html>

Resolve a Deferred

Call resolve() when the asynchronous operation completes successfully, and any done() callbacks already attached — or attached later — will receive whatever values you pass to resolve(). This is the signal that tells all the listening code the task finished the way it was supposed to.

Example: Resolve a Deferred

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      const dfd = $.Deferred();
      dfd.done(function(value) { console.log(value); });
      dfd.resolve("Success value");
    </script>
  </body>
</html>

Reject a Deferred

Call reject() when the asynchronous operation fails, and any fail() callbacks receive the values passed to reject(), typically an error message or object describing what went wrong. This lets calling code distinguish a genuine failure from a successful result without needing separate return paths.

Example: Reject a Deferred

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      const dfd = $.Deferred();
      dfd.fail(function(error) { console.log(error); });
      dfd.reject("Something went wrong");
    </script>
  </body>
</html>

Resolve After a Delay

A Deferred can represent any delayed operation, not just an AJAX call — a setTimeout, a user interaction, or a long-running calculation can all be wrapped in one and resolved once the actual work finishes. This makes Deferred a general-purpose tool for turning any async pattern into something with a consistent done/fail interface.

Example: Resolve After a Delay

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      function delayedTask() {
      const dfd = $.Deferred();
      setTimeout(function() { dfd.resolve("Finished"); }, 200);
      return dfd;
      }
      delayedTask().done(function(msg) { console.log(msg); });
    </script>
  </body>
</html>

Return a Promise

Return d.promise() — not the Deferred itself — when outside code should be able to observe the task's outcome but shouldn't be able to resolve or reject it directly. This protects the internal resolve/reject logic so only the code that created the Deferred controls when it settles.

Example: Return a Promise

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      function loadData() {
      const dfd = $.Deferred();
      setTimeout(function() { dfd.resolve("data"); }, 100);
      return dfd.promise();
      }
      const result = loadData();
      result.done(function(data) { console.log(data); });
    </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.