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

Deferred Objects का Introduction

एक Deferred object किसी asynchronous operation को represent करता है। यह दूसरे code को notify कर सकता है कि operation succeed हुआ या fail।
Syntax
javascript
var dfd = $.Deferred();

dfd.done(function(value) { /* success */ });
dfd.fail(function(reason) { /* failure */ });
dfd.always(function() { /* either way */ });

dfd.resolve(value);   // or dfd.reject(reason);

एक Deferred Object क्या है?

एक Deferred object किसी asynchronous task को wrap करता है और यह announce करने के लिए methods expose करता है कि वह task आख़िरकार succeed हुआ या fail।

उदाहरण: What is a Deferred Object?

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>

Deferred क्यों इस्तेमाल करें?

Deferred objects किसी asynchronous task को उस code से अलग रखने में मदद करते हैं जो आख़िरकार उसका result इस्तेमाल करता है, ताकि task शुरू करने वाले code को जानने की ज़रूरत न हो।

उदाहरण: Why Use Deferred?

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 ready"); }, 100);
      return dfd.promise();
      }
      loadData().done(function(result) { console.log(result); });
    </script>
  </body>
</html>

Deferred और Callbacks

done() successful completion के लिए एक callback register करता है, fail() rejection के लिए एक register करता है, और always() एक callback register करता है जो चाहे कुछ भी हो चलता है।

उदाहरण: Deferred and Callbacks

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() { console.log("Success"); });
      dfd.fail(function() { console.log("Failed"); });
      dfd.always(function() { console.log("Always runs"); });
      dfd.resolve();
    </script>
  </body>
</html>

Deferred Promise

एक promise resolve या reject expose किए बिना किसी Deferred के done, fail, और always methods expose करता है, ताकि सिर्फ result observe करने वाला code access न कर सके।

उदाहरण: Deferred Promise

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

एक छोटा Deferred Project

Deferred objects asynchronous flow को explicitly देखने का एक अच्छा तरीका हैं: task तुरंत शुरू होता है, लेकिन इसका outcome सिर्फ बाद में report होता है जब यह।

उदाहरण: Small Deferred Project

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("Task started");
      setTimeout(function() {
      dfd.resolve("Task complete");
      }, 200);
      dfd.done(function(msg) { console.log(msg); });
    </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. resolve() call करना और यह उम्मीद करना कि बाद में add किए गए done() callbacks ignore हो जाएँगे, जबकि jQuery उन्हें तुरंत चला देता है।
  2. callers को Deferred खुद return करना, उन्हें resolve() call करने देते हुए, जबकि आपको dfd.promise() return करना चाहिए।
  3. कहीं भी resolve() या reject() call करना भूल जाना, ताकि callbacks कभी न चलें और task pending रहे।
🔒

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.