Promises in JavaScript
Index
- What is a Promise? (Promise क्या है?)
- Syntax of a Promise (Promise का Syntax)
- Using a Promise (Promise का उपयोग)
- Real-Life Example (Asynchronous Example with Promises)
- Chaining Promises (Promise Chaining)
- Handling Multiple Promises (Promise.all)
- Key Points (मुख्य बातें)
- Summary
What is a Promise? (Promise क्या है?)
Promises JavaScript में asynchronous operations को handle करने का एक तरीका है। Promises का उपयोग तब किया जाता है जब कोई task asynchronous तरीके से complete होता है और हमें उसके successful completion या failure का इंतजार करना पड़ता है। यह हमें callbacks के बजाय एक structured और readable code लिखने में मदद करता है।
Syntax of a Promise (Promise का Syntax)
let myPromise = new Promise(function(resolve, reject) {
// Asynchronous code here
let success = true;
if (success) {
resolve("Task completed successfully!");
} else {
reject("Task failed!");
}
});
resolve(): यह function तब call किया जाता है जब task successfully complete होता है।
reject(): यह function तब call किया जाता है जब task fail हो जाता है।
Using a Promise (Promise का उपयोग)
Promises को handle करने के लिए हम .then()
और .catch()
methods का उपयोग करते हैं।
- .then(): यह method तब execute होता है जब promise fulfilled (success) होता है।
- .catch(): यह method तब execute होता है जब promise rejected (fail) होता है।
Example: Basic Promise
let myPromise = new Promise(function(resolve, reject) {
let taskCompleted = true;
if (taskCompleted) {
resolve("The task was successful.");
} else {
reject("The task failed.");
}
});
myPromise.then(function(result) {
console.log(result); // Output: The task was successful.
}).catch(function(error) {
console.log(error); // If rejected: Output: The task failed.
});
Real-Life Example (Asynchronous Example with Promises)
मान लीजिए आप एक API से data fetch कर रहे हैं, जो asynchronous process है। इस case में आप promise का उपयोग करके response का इंतजार कर सकते हैं।
Example: Fetching Data from API
let fetchData = new Promise(function(resolve, reject) {
let success = true; // Imagine this is API success response
setTimeout(() => {
if (success) {
resolve("Data fetched successfully!");
} else {
reject("Failed to fetch data.");
}
}, 2000); // Simulate async task with 2 seconds delay
});
fetchData.then(function(data) {
console.log(data); // Output: Data fetched successfully!
}).catch(function(error) {
console.log(error); // Output: Failed to fetch data.
});
Chaining Promises (Promise Chaining)
Promise chaining तब useful होती है जब आपको एक के बाद एक multiple asynchronous tasks को execute करना हो। हर .then()
block अगले asynchronous task को execute करता है।
Example: Promise Chaining
let myPromise = new Promise(function(resolve, reject) {
resolve("Step 1 complete");
});
myPromise.then(function(result) {
console.log(result);
return "Step 2 complete"; // Pass this to the next .then()
}).then(function(result) {
console.log(result);
return "Step 3 complete";
}).then(function(result) {
console.log(result);
}).catch(function(error) {
console.log(error); // Handle any errors in the chain
});
Output:
Step 1 complete
Step 2 complete
Step 3 complete
Handling Multiple Promises (Promise.all)
अगर आपको multiple promises को एक साथ execute करना है और सभी के complete होने का इंतजार करना है, तो आप Promise.all()
का उपयोग कर सकते हैं।
Example: Handling Multiple Promises with Promise.all()
let promise1 = new Promise((resolve) => resolve("Task 1 done"));
let promise2 = new Promise((resolve) => resolve("Task 2 done"));
let promise3 = new Promise((resolve) => resolve("Task 3 done"));
Promise.all([promise1, promise2, promise3]).then((results) => {
console.log(results); // Output: ["Task 1 done", "Task 2 done", "Task 3 done"]
}).catch((error) => {
console.log(error); // If any promise fails, this will handle the error
});
Key Points (मुख्य बातें)
- Promises asynchronous operations को manage करने के लिए use होते हैं।
- Promises की तीन states होती हैं: pending, fulfilled, और rejected।
- Promises को handle करने के लिए
.then()
और.catch()
methods का उपयोग किया जाता है। - Promise Chaining के ज़रिए आप multiple asynchronous tasks को sequentially execute कर सकते हैं।
- Multiple promises को parallel में handle करने के लिए
Promise.all()
का use होता है।
Summary
JavaScript में Promises asynchronous operations को manage करने का structured और readable तरीका प्रदान करते हैं। Promises asynchronous tasks के result का इंतजार करते हैं और फिर success या failure के आधार पर different actions लेते हैं। Promises की flexibility से आप complex asynchronous flows को भी आसानी से handle कर सकते हैं।
No comments:
Post a Comment