Deferred Objects का Introduction
In this page:
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?
<!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?
<!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
<!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
<!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
<!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>
resolve()call करना और यह उम्मीद करना कि बाद में add किए गएdone()callbacks ignore हो जाएँगे, जबकि jQuery उन्हें तुरंत चला देता है।- callers को Deferred खुद return करना, उन्हें
resolve()call करने देते हुए, जबकि आपकोdfd.promise()return करना चाहिए। - कहीं भी
resolve()याreject()call करना भूल जाना, ताकि callbacks कभी न चलें और task pending रहे।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: