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

Promise Pipeline Methods

done successful completion handle करता है। fail rejection handle करता है। इन्हें एक promise से attach किया जा सकता है।
Syntax
javascript
deferred.promise()
  .then(doneCallback, failCallback)
  .done(callback)
  .fail(callback)
  .always(callback);

done और fail

done() और fail() ऐसे callbacks register करते हैं जिन्हें jQuery automatically तब call करता है जब कोई Deferred successfully resolve हो जाता है या reject हो जाता है।

उदाहरण: done and fail

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("Resolved"); });
      dfd.fail(function() { console.log("Rejected"); });
      dfd.resolve();
    </script>
  </body>
</html>

then Method

then() method एक ही call में success और failure दोनों handlers register कर सकता है, और — importantly — यह एक नया promise return करता है, जो आपको chain करने देता है।

उदाहरण: then Method

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.then(function() { console.log("Success handler"); }, function() { console.log("Failure handler"); });
      dfd.resolve();
    </script>
  </body>
</html>

always Method

always() चाहे Deferred resolve हो या reject, बहरहाल चलता है, जो loading spinner छुपाने या फिर से enable करने जैसे cleanup work के लिए natural जगह बनाता है।

उदाहरण: always Method

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.always(function() { console.log("Cleanup runs either way"); });
      dfd.resolve();
    </script>
  </body>
</html>

Async Work के साथ Pipeline

Promise methods कई asynchronous steps को साथ जोड़ सकते हैं, जहाँ हर step का then() handler एक value — या दूसरा promise — return करता है जो अगला step बनता है।

उदाहरण: Pipeline with Async Work

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      $.Deferred().resolve(5).then(function(n) { return n * 2; }).then(function(n) { console.log("Result:", n); });
    </script>
  </body>
</html>

एक छोटा Promise Pipeline Project

इस तरह बनाई गई एक pipeline asynchronous steps की एक sequence को predictable order में चलती रखती है, हर step का result अगले में cleanly flow करते हुए।

उदाहरण: Small Promise Pipeline Project

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      function step1() { return $.Deferred().resolve("step1").promise(); }
      step1()
      .then(function(r) { console.log(r); return "step2"; })
      .then(function(r) { console.log(r); });
    </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. यह उम्मीद करना कि .done() chain में नीचे pass की गई value बदल देगा, जबकि सिर्फ .then() अगले step के लिए एक नई value या promise return कर सकता है।
  2. सिर्फ .done() add करना और .fail() या .always() भूल जाना, ताकि rejected tasks silently ignore हो जाएँ।
  3. यह मान लेना कि .then() jQuery 3 से पहले exactly native Promise जैसा behave करता है, जबकि पुराने versions thrown errors को उसी तरह catch नहीं करते थे।
🔒

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.