Promise
प्याला एक पैराग्राफ है, #cup. शुरू में blank है, और स्क्रिप्ट चाय माँगने से पहले उसे waiting लिख देती है।
pour पेय नहीं लौटाता। एक Promise लौटाता है। then से तुम उस Promise से कहते हो कि पेय बाद में लिखे। pour के नीचे की पंक्ति अभी waiting कहती है। पंक्ति waiting / lida tea बनती है।
अभी परोसा गया प्याला नहीं
पिछला पेज done को serve को देता था। serve लौटने से पहले फ़ंक्शन बुलाता था, इसलिए नीचे का लॉग पहले ही lida tea / poured था। वहाँ कुछ इंतज़ार नहीं करता था।
यहाँ then को दिया फ़ंक्शन अगली पंक्ति में नहीं चलता। pour पहले लौटता है। पेय तब दिखता है जब यह स्क्रिप्ट खत्म हो चुकी हो।
तीन स्थितियाँ
Promise pending रहता है जब तक तय न हो। resolve उसे एक मान से पूरा करता है। reject उसे एक कारण से अस्वीकार करता है। then सिर्फ़ पूरा होने पर चलता है। catch सिर्फ़ अस्वीकार पर चलता है। इस पेज पर टाइमर नहीं है। executor तुरंत resolve या reject बुलाता है, और handler फिर भी स्क्रिप्ट के अंत तक रुकते हैं।
📊 तीन स्थितियाँ
| स्थिति | क्या दिखता है | आसानी से उलझता है |
|---|---|---|
| pending | प्याला अभी waiting कहता है | पेय पहले से प्याले पर है |
| fulfilled | then को lida tea मिलता है | catch भी चले |
| rejected | catch को no leaves मिलता है | then को पेय मिले |
executor अभी है, handler नहीं
new Promise को दिया फ़ंक्शन तुरंत चलता है। इसलिए resolve("lida tea") pour के लौटने से पहले हो सकता है। then वाला फ़ंक्शन अलग है। वह कतार में जाता है। pour(...) के बाद की पंक्ति तब चलती है जब प्याला अभी waiting कह रहा हो।
अगली पंक्ति अभी इंतज़ार में है
pour("lida") एक Promise को order में रखता है। प्याला waiting हो चुका है। लॉग वही शब्द है, पेय नहीं।
function pour(order) {
return new Promise(function (resolve, reject) {
if (order === "") {
reject("no leaves");
return;
}
resolve(order + " tea");
});
}
let cup = document.getElementById("cup");
cup.textContent = "waiting";
let order = pour("lida");
console.log(cup.textContent);
लॉग waiting है। order Promise है। वह lida tea टेक्स्ट नहीं है।
then पेय पाता है
then को एक फ़ंक्शन दो। वह फ़ंक्शन resolve को दिया मान पाता है। पेय लिखता है और लॉग करता है। यह लॉग अगली पंक्ति का नहीं है। यह then के अंदर का लॉग है।
function pour(order) {
return new Promise(function (resolve, reject) {
if (order === "") {
reject("no leaves");
return;
}
resolve(order + " tea");
});
}
let cup = document.getElementById("cup");
pour("lida").then(function (drink) {
cup.textContent = drink;
console.log(cup.textContent);
});
लॉग lida tea है। खाली टेक्स्ट इस handler तक नहीं पहुँचता।
पत्ते नहीं हैं
pour("") reject("no leaves") बुलाकर लौटता है। catch वह कारण पाता है। then नहीं चलता।
function pour(order) {
return new Promise(function (resolve, reject) {
if (order === "") {
reject("no leaves");
return;
}
resolve(order + " tea");
});
}
pour("").catch(function (reason) {
console.log(reason);
});
लॉग no leaves है।
Promise पेय नहीं है
order.then एक फ़ंक्शन है। इससे पता चलता है कि order Promise है, माँगा टेक्स्ट नहीं। Promise की "lida tea" से तुलना गलत होती, और यह टुकड़ा सिर्फ़ then का प्रकार छापता है।
function pour(order) {
return new Promise(function (resolve, reject) {
if (order === "") {
reject("no leaves");
return;
}
resolve(order + " tea");
});
}
let order = pour("lida");
console.log(typeof order.then);
लॉग function है।
प्याले के नीचे की पंक्ति
स्क्रिप्ट प्याले को waiting करती है और वह शब्द तुरंत जोड़ती है। then lida tea जोड़ता है और दोनों शब्द जोड़ देता है। जोड़ना then के अंदर है, इसलिए स्क्रिप्ट के बाद होता है, अगली पंक्ति में नहीं।
<!DOCTYPE html>
<html>
<head>
<title>promised-cup — page</title>
</head>
<body>
<p id="cup">blank</p>
<p id="line">…</p>
<script>
function pour(order) {
return new Promise(function (resolve, reject) {
if (order === "") {
reject("no leaves");
return;
}
resolve(order + " tea");
});
}
let cup = document.getElementById("cup");
let seen = [];
cup.textContent = "waiting";
seen.push(cup.textContent);
pour("lida").then(function (drink) {
seen.push(drink);
cup.textContent = drink;
document.getElementById("line").textContent = seen.join(" / ");
});
</script>
</body>
</html>
पंक्ति waiting / lida tea है। waiting तुरंत था। lida tea then में आया। blank शब्द चला गया। lida tea / poured पिछला पेज है।
काउंटर पर
- प्याले को waiting करो,
pour("lida")बुलाओ, और प्याला तुरंत लॉग करो। लॉग waiting है। thenके अंदर पेय प्याले पर लिखो। वह लॉग lida tea है।pour("")बुलाओ और कारणcatchसे पकड़ो। लॉग no leaves है।typeof order.thenपढ़ो। लॉग function है।- पेज की पंक्ति पढ़ो: waiting / lida tea.
काउंटर पर चूक
then चलने तक प्याला waiting कहता है। poured नहीं कहता।
- यह उम्मीद कि
pourके नीचे का लॉग lida tea हो - लौटाई मान को पेय का टेक्स्ट समझना
- खाली ऑर्डर पर
thenलगाना और no leaves छोड़ देना - lida पर
catchलगाना और no leaves की उम्मीद करना - पंक्ति को lida tea / poured पढ़ना
यह याद रखो
pourPromise लौटाता है, lida tea टेक्स्ट नहीं- अगली पंक्ति अभी भी waiting लॉग करती है
thenइस स्क्रिप्ट के बाद lida tea पाता है- खाली ऑर्डर no leaves से अस्वीकार होता है, और
catchउसे लॉग करता है - पेज की पंक्ति waiting / lida tea है
🧠 अपने ज्ञान की परीक्षा करें
अपने ज्ञान की परीक्षा करें
इस इंटरैक्टिव क्विज़ के साथ अपनी चुनौती लें और देखें कि आप विषय को कितनी अच्छी तरह समझते हैं
📝 निर्देश
- हर प्रश्न को ध्यान से पढ़ें
- हर प्रश्न के लिए सबसे अच्छा उत्तर चुनें
- आप जितनी बार चाहें क्विज़ दोबारा दे सकते हैं
- आपकी प्रगति शीर्ष पर दिखाई जाएगी