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

Deferred बनाना और Resolve करना

एक Deferred object बनाने के लिए $.Deferred इस्तेमाल करें। यह pending state में शुरू होता है।
Syntax
javascript
var deferred = $.Deferred();
deferred.resolve(value);
deferred.reject(reason);
deferred.promise();

एक Deferred बनाना

$.Deferred() एक नया Deferred object बनाता है जो एक ऐसे task को represent करता है जो अभी resolve या reject नहीं हुआ है। आप आमतौर पर एक function की शुरुआत में एक बनाते हैं।

उदाहरण: 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>

एक Deferred को Resolve करना

जब asynchronous operation successfully complete हो जाए तो resolve() call करें, और पहले से attached — या बाद में attached — कोई भी done() callbacks उसे मिलेंगे जो।

उदाहरण: 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>

एक Deferred को Reject करना

जब asynchronous operation fail हो जाए तो reject() call करें, और कोई भी fail() callbacks reject() को pass की गई values receive करते हैं, आमतौर पर एक error message या object।

उदाहरण: 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>

एक Delay के बाद Resolve करना

एक Deferred सिर्फ AJAX call ही नहीं, किसी भी delayed operation को represent कर सकता है — एक setTimeout, एक user interaction, या एक long-running calculation सब wrap किए जा सकते हैं।

उदाहरण: 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>

एक Promise Return करना

d.promise() return करें — Deferred खुद नहीं — जब बाहरी code task के outcome को observe कर पाए लेकिन उसे resolve या reject न कर पाए।

उदाहरण: 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>
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. dfd.resolve() को गलत scope में call करना ताकि यह कभी न चले, जैसे इसे setTimeout callback के अंदर call करना भूल जाना।
  2. resolve(a, b) को कई values pass करना और फिर done(function (x) {...}) में सिर्फ पहली पढ़ना, दूसरे argument को miss करते हुए।
  3. function से dfd.promise() return किए बिना new $.Deferred call करना, ताकि caller के पास done() attach करने के लिए कुछ भी न हो।
🔒

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.