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

Deferred States

एक नया Deferred pending state में शुरू होता है। इसे अभी तक resolve या reject नहीं किया गया है।
Syntax
javascript
var dfd = $.Deferred();      // state: "pending"

dfd.resolve(value);          // state: "resolved" -> done() runs
dfd.reject(reason);          // state: "rejected" -> fail() runs

dfd.state();                 // read the current state

Pending State

हर नया Deferred pending state में शुरू होता है, यानी इसका underlying task अभी किसी भी direction में खत्म नहीं हुआ है। Pending हमेशा शुरुआती state होती है।

उदाहरण: Pending State

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(dfd.state());
    </script>
  </body>
</html>

Resolved State

जब किसी Deferred पर resolve() call किया जाता है, यह resolved state में चला जाता है, और उससे attached कोई भी done() callbacks उसे pass की गई values के साथ तुरंत call हो जाते हैं।

उदाहरण: Resolved State

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("done!");
    </script>
  </body>
</html>

Rejected State

जब किसी Deferred पर reject() call किया जाता है, यह rejected state में चला जाता है, और उससे attached कोई भी fail() callbacks reject() को pass की गई values के साथ call हो जाते हैं।

उदाहरण: Rejected State

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(reason) { console.log(reason); });
      dfd.reject("something failed");
    </script>
  </body>
</html>

State वापस नहीं जा सकती

एक बार कोई Deferred resolve या reject हो जाए, इसकी state वापस pending में नहीं जा सकती या दूसरे outcome में switch नहीं हो सकती — final state permanent है।

उदाहरण: State Cannot Go Back

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.resolve();
      console.log(dfd.state());
      dfd.reject();
      // has no effect, already resolved
    </script>
  </body>
</html>

किसी Task में State Check करना

state() method current state को एक string (pending, resolved, या rejected) के रूप में return करता है, जो तब उपयोगी है जब आपको check करना हो कि कोई async।

उदाहरण: Check State in a Task

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      const dfd = $.Deferred();
      if (dfd.state() === "pending") {
      console.log("Still working");
      }
    </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. #}
आम गलतियां
  1. किसी Deferred की state settle हो जाने के बाद उसे बदलने की कोशिश करना, जैसे resolve() के बाद reject() call करना, जबकि state final है और दूसरी call ignore हो जाती है।
  2. async work शुरू करने के तुरंत बाद dfd.state() check करना और "resolved" की उम्मीद करना, जबकि यह अभी भी "pending" है।
  3. "rejected" को code में throw हुए किसी error से confuse करना, जबकि इसका मतलब सिर्फ यह है कि reject() call किया गया था।
🔒

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.