JS प्रॉमिसेस
In this page:
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 में, यानी असफल हुआ।
- किसी promise की तीन states को किसी package की shipping status की तरह सोचें:
- pending का मतलब अभी transit में है
- fulfilled का मतलब deliver हो गया
- rejected का मतलब खो गया।
उदाहरण: What Is a Promise
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 चलाता है।
उदाहरण: then, catch, and finally
// 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 कर सकते हैं।
उदाहरण: Chaining Promises
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 हो जाता है।
उदाहरण: Promise.all
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 करता है।
उदाहरण: Promise.race
Promise.race([
new Promise(resolve => setTimeout(() => resolve("slow"), 100)),
new Promise(resolve => setTimeout(() => resolve("fast"), 10)),
]).then(result => console.log(result)); // "fast"
- errors handle करने के लिए .catch() जोड़ना भूल जाना, एक unhandled promise rejection चुपचाप fail हो सकती है या console को warnings से भर सकती है।
- chain करने के बजाय .then() calls को गहराई से nest करना, जो एक flat chain के मुकाबले जल्दी पढ़ने में मुश्किल हो जाता है।
- यह मान लेना कि कोई 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 हिस्सा हैं।
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic