Deferred बनाना और Resolve करना
In this page:
var deferred = $.Deferred();
deferred.resolve(value);
deferred.reject(reason);
deferred.promise();
एक Deferred बनाना
$.Deferred() एक नया Deferred object बनाता है जो एक ऐसे task को represent करता है जो अभी resolve या reject नहीं हुआ है। आप आमतौर पर एक function की शुरुआत में एक बनाते हैं।
उदाहरण: Create a Deferred
<!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
<!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
<!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
<!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
<!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>
dfd.resolve()को गलत scope में call करना ताकि यह कभी न चले, जैसे इसेsetTimeoutcallback के अंदर call करना भूल जाना।resolve(a, b)को कई values pass करना और फिरdone(function (x) {...})में सिर्फ पहली पढ़ना, दूसरे argument को miss करते हुए।- function से
dfd.promise()return किए बिनाnew $.Deferredcall करना, ताकि caller के पासdone()attach करने के लिए कुछ भी न हो।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: