← Back to JavaScript Course | Chapter 4: Modern JS, Async & DOM | Lesson 16 of 26

JS प्रॉमिसेस

किसी restaurant में खाना order करने के बारे में सोचें, आपको तुरंत खाना नहीं मिलता, server आपको एक receipt देता है, यह promise कि खाना आ रहा है, और आखिरकार वह promise या तो आपके खाने के आने में resolve होता है या kitchen आपको बताती है कि उनके पास कोई ingredient नहीं है। JavaScript का Promise भी बिल्कुल वैसे ही काम करता है, यह एक ऐसी value को represent करता है जो अभी उपलब्ध नहीं है पर किसी समय पर होगी, चाहे सफलतापूर्वक (fulfilled) या असफल रूप से (rejected), और यह आपके code को उस outcome के पता चलने पर react करने देता है। Promises किसी भी ऐसी चीज़ को संभालने के लिए ज़रूरी हैं जिसमें समय लगता है, जैसे cookiescursor.com पर पर्दे के पीछे किसी server से tutorial data fetch करना, वो भी इंतज़ार करते हुए बाकी page को freeze किए बिना।
Syntax
javascript
const promise = new Promise((resolve, reject) => {
  // resolve(value) or reject(error)
});

promise.then(value => {}).catch(error => {}).finally(() => {});

Promise क्या है

एक Promise एक ऐसा object है जो किसी ऐसे operation के आखिरी result को represent करता है जो अभी खत्म नहीं हुआ है, यह एक pending state से शुरू होता है, और आखिरकार या तो fulfilled में settle होता है, यानी सफल हुआ, या rejected में, यानी असफल हुआ।

Note:
  • किसी promise की तीन states को किसी package की shipping status की तरह सोचें:
  • pending का मतलब अभी transit में है
  • fulfilled का मतलब deliver हो गया
  • rejected का मतलब खो गया।
Warning: एक promise सिर्फ एक बार settle हो सकता है, या तो fulfilled या rejected, कभी दोनों नहीं, और settle होने के बाद उसकी state फिर कभी नहीं बदल सकती।

उदाहरण: What Is a Promise

javascript
const promise = new Promise((resolve, reject) => {
  resolve("Success!");
});
console.log(promise); // Promise {<fulfilled>: 'Success!'}

then, catch, और finally

.then() किसी promise के सफलतापूर्वक fulfill होने पर एक function चलाता है, .catch() किसी promise के reject होने पर एक function चलाता है, और .finally() चाहे promise सफल हुआ हो या असफल, हर हाल में एक function चलाता है।

Note: .finally() cleanup logic के लिए एक बढ़िया जगह है, जैसे loading spinner छिपाना, क्योंकि यह हमेशा चलता है चाहे promise कैसे भी settle हो।
Warning: .catch() को पूरी तरह छोड़ देने का मतलब है कि errors unhandled रह जाते हैं, जिससे आपकी application में confusing, silent failures हो सकते हैं।

उदाहरण: then, catch, and finally

javascript
// Declare the constant `promise` as a new `Promise` instance
// Declare the constant `promise` as a new `Promise` instance
const promise = new Promise((resolve) => resolve("Done"));
promise
  // On success, run this with the resolved value as `result`
  // On success, run this with the resolved value as `result`
  .then(result => console.log(result))
  // On failure, run this with the error as `err`
  // On failure, run this with the error as `err`
  .catch(err => console.log(err))
  .finally(() => console.log("Finished"));

Promises को Chain करना

कई .then() calls को एक साथ chain किया जा सकता है, हर एक को पिछले वाले से return हुई value मिलती है, जिससे आप asynchronous steps के एक sequence को साफ, readable, ऊपर-से-नीचे क्रम में express कर सकते हैं।

Note: किसी .then() के अंदर से कोई value return करना उसे अपने आप chain के अगले .then() तक पहुंचा देता है।
Warning: किसी .then() callback के अंदर से कोई value return करना भूल जाना, chain के अगले step को expected data के बजाय undefined दिला देता है।

उदाहरण: Chaining Promises

javascript
Promise.resolve(1)
  .then(n => n + 1)
  .then(n => n * 2)
  .then(result => console.log(result)); // 4

Promise.all

Promise.all() promises की एक array लेता है और एक नया single promise return करता है जो उन सबके fulfill होते ही उन सभी के results की एक array के साथ fulfill हो जाता है, या अगर उनमें से कोई एक भी reject हो जाए तो तुरंत reject हो जाता है।

Note: Promise.all() तब आदर्श है जब आपको आगे बढ़ने से पहले कई independent operations के पूरे होने की ज़रूरत हो, जैसे कई resources को एक साथ load करना।
Warning: अगर Promise.all() को पास किया गया एक भी promise reject होता है, तो पूरा combined promise तुरंत reject हो जाता है, भले ही बाकी सफल होते।

उदाहरण: Promise.all

javascript
Promise.all([
  Promise.resolve(1),
  Promise.resolve(2),
  Promise.resolve(3),
]).then(results => console.log(results)); // [1, 2, 3]

Promise.race

Promise.race() promises की एक array लेता है और जैसे ही उनमें से सबसे पहला settle होता है, चाहे वह fulfill हो या reject, वैसे ही settle हो जाता है, यह उन्हें असल में एक-दूसरे से race कराता है और सबसे तेज़ outcome return करता है।

Note: Promise.race() timeout लागू करने की एक आम technique है, किसी असली operation को ऐसे promise के खिलाफ race कराना जो एक तय delay के बाद reject होता है।
Warning: Promise.race() सिर्फ speed के आधार पर settle होता है, यह यह confirm करने के लिए नहीं रुकता कि कोई तेज़ rejection असल में किसी धीमी success से कम महत्वपूर्ण था या नहीं।

उदाहरण: Promise.race

javascript
Promise.race([
  new Promise(resolve => setTimeout(() => resolve("slow"), 100)),
  new Promise(resolve => setTimeout(() => resolve("fast"), 10)),
]).then(result => console.log(result)); // "fast"
Live Example
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. #}
आम गलतियां
  1. errors handle करने के लिए .catch() जोड़ना भूल जाना, एक unhandled promise rejection चुपचाप fail हो सकती है या console को warnings से भर सकती है।
  2. chain करने के बजाय .then() calls को गहराई से nest करना, जो एक flat chain के मुकाबले जल्दी पढ़ने में मुश्किल हो जाता है।
  3. यह मान लेना कि कोई promise तुरंत resolve हो जाता है, promise बनाने के बाद का code उसका result तैयार होने से पहले चलता है, यही इसे इस्तेमाल करने का पूरा मकसद है।
चैप्टर सारांश
  • एक promise एक ऐसी value represent करता है जो बाद में उपलब्ध होगी, तीन states में से किसी एक में मौजूद: pending, fulfilled, या rejected।
  • .then() एक successful result handle करता है, .catch() एक error handle करता है, और .finally() outcome चाहे जो भी हो चलता है।
  • Promise.all() कई promises के एक साथ खत्म होने का इंतज़ार करता है, और Promise.race() पहले वाले के खत्म होते ही resolve हो जाता है।
ब्राउज़र सपोर्ट

Promises को 2015 से सभी major browsers में support मिला है और ये modern JavaScript का पूरी तरह standard हिस्सा हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.