Promises क्या हैं
एक promise एक ऐसी value के लिए stand करता है जो बाद में exist करेगी: यह pending, fulfilled, या rejected हो सकती है।
In this page:
Syntax
promise
.then((result) => {
// success
})
.catch((err) => {
// failure
})
.finally(() => {
// always runs
});
Promises
एक promise का then success handle करता है, catch failure handle करता है, और finally दोनों case में चलता है। Handlers नई promises return करते हैं ताकि आप इन्हें chain कर सकें। new Promise से promises बनाना callback APIs wrap करता है।
Note:
हमेशा chains को catch से खत्म करें ताकि rejections unhandled न रहें।
उदाहरण: Promises
const p = new Promise((resolve, reject) => {
setTimeout(() => resolve(21), 5);
});
p.then((n) => n * 2)
.then((n) => { console.log("result:", n); throw new Error("oops"); })
.catch((e) => console.log("caught:", e.message))
.finally(() => console.log("cleanup"));
// Output:
// result: 42
// caught: oops
// cleanup
⚠️ Run this in your own terminal or Node.js environment.
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. #}
आम गलतियां
- then के अंदर return करना भूल जाना
- rejections को unhandled छोड़ना
- बेवजह एक promise के अंदर एक promise बनाना
चैप्टर सारांश
- States: pending, fulfilled, rejected होते हैं
- then, catch, और finally results handle करते हैं
- Chains नई promises return करती हैं
- हमेशा catch add करें
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: