Table of Contents
Introduction
हेलो दोस्तों आज हम इस post में javascript में promises के बारेमे जानेगे की promises क्या है और उसका इस्तेमाल कब और कैसे करते है वह हम इस post में जानेगे!.
What are promises in javascript in Hindi?
Javascript में Promise asynchronous operation का success और failure object का representing करता है!.
Promises javascript का core feature है जो asynchronous task को structured way से handle karta है जैसे की server से data को fetch करना, reading file और ऐसे कई operation जो complate होने में time लेते है!.
Promises एक asynchronous take के लिए हमें promise करता है जो अभी तक complate नहीं हुवा है लेकिन feature में किसी point पर ख़तम होगा. और promise इन तीन states में से एक में काम करता है!.
Pending: यह promise का initial state है, जब asynchronous operation अभी बी progress पर है और finish नहीं हुवा है!.
Fulfilled: जब promise successfully पूरा हो गया है, और resulting value available है तब promises Fulfilled state में होता है!.
Rejected: जब promise failed हो गया हो और failed का कारन available हो तब promises Rejected state में होता है!.
Promises का इस्तेमाल javascript में क्यों किया जाता है?
Promises का उपयोग asynchronous code को read और manage करने में आसान बनाने के लिए किया जाता है!. पहले javascript में callback का इस्तेमाल करके asynchronous task को manage किया जाता था लेकिन “callback hell” जैसी issues होती थी deelply से nested और code को manage करने में difficult होता था!. Promises asynchronous code को manage करने के लिए एक clean, readable and flexibility provide करके इस issue को manage करता है!.
Promises का इस्तेमाल करने के main reasons
Avoiding Callback Hell: Promises आपको linear, readable तरीके से asynchronous operation के chaining करने की अनुमति देता है, जिससे आपका code proper structured और error को easly manage कर सकते है!.
Better Error Handling: Promises .cathc() के साथ error को handle करने का एक strucuted तरीका provide करते है!.
Chaining: Promises को .then() का इस्तेमाल करके एक साथ chained किया जा सकता है, जिससे कई asynchronous tasks को deeply से nested callback create बिना sequence में executed किया जा सकता है!.
Clean Syntex: Promises asynchronous task के साथ अधिक synchronous जैसे flow को सक्षम करके code को readability में improved करते है!.
Promises कैसे work करते है?
जब बी promise create होता है, तो यह pending state में start होता है!. और यह last में settles हो जाता है या तो यह fulfilled और rejected state में होता है!. जिसके बाद वह result show करेगा या errors.
Promise constructor का इस्तेमाल करके एक promise create किया जाता है!. जो executor name का एक function लेता है!. executor function तुरंत executed होता है और दो parameters लेता है resolve और reject.
resolve: जब operation successfully complete हो जाता है तब resolve function call होता है!.
reject: जब operation fails हो जाता है तब यह function call होता है!.
Promises को कैसे handle किया जाता है?
Promises को handle करने के लिए main methods
then(): यह, वह define करने के लिए इस्तेमाल किया जाता है की जब promise fulfilled होता है तो क्या होता है!. यह दो parameters लेता है!. एक जब promise success हो जाता है और एक जब इसे rejected कर दिया जाता है!.
catch(): यह define करने के लिए इस्तेमाल किया जाता है की जब promise rejected कर दिया जाता है तो क्या होता है यह error को cathes करता है और उन्हें handles करता है!.
finally(): यह method तब call किया जाता है जब promise settled हो जाता है, फिर बाले ही इसे पूरा किया गया हो या reject किया गया हो!.
Promise Example
Simple Promise Example
// Creating a new Promise let myPromise = new Promise((resolve, reject) => { // Simulating an asynchronous task (e.g., fetching data) let success = true; setTimeout(() => { if (success) { resolve("Data fetched successfully!"); // Task succeeded } else { reject("Failed to fetch data!"); // Task failed } }, 2000); // Simulating 2 seconds delay }); // Handling the Promise myPromise .then((result) => { console.log(result); // If promise is fulfilled, this will run }) .catch((error) => { console.error(error); // If promise is rejected, this will run }) .finally(() => { console.log("Promise settled (either fulfilled or rejected)."); });
Chaining Promises Example
let asyncTask1 = new Promise((resolve, reject) => { setTimeout(() => { resolve("Task 1 completed"); }, 1000); }); let asyncTask2 = new Promise((resolve, reject) => { setTimeout(() => { resolve("Task 2 completed"); }, 2000); }); asyncTask1 .then((result1) => { console.log(result1); // Logs "Task 1 completed" after 1 second return asyncTask2; // Return the next promise to chain }) .then((result2) => { console.log(result2); // Logs "Task 2 completed" after another 2 seconds }) .catch((error) => { console.error(error); // Handles any errors in the chain });
Real-World Usage Promise
fetch("https://api.example.com/data") .then(response => response.json()) // Parsing the JSON data from the response .then(data => { console.log(data); // Logs the fetched data }) .catch(error => { console.error("Error fetching data:", error); // Handles any errors });