Promise Pipeline Methods
In this page:
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
<!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
<!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
<!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
<!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
<!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>
- यह उम्मीद करना कि
.done()chain में नीचे pass की गई value बदल देगा, जबकि सिर्फ.then()अगले step के लिए एक नई value या promise return कर सकता है। - सिर्फ
.done()add करना और.fail()या.always()भूल जाना, ताकि rejected tasks silently ignore हो जाएँ। - यह मान लेना कि
.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: