Jul 13, 2024
let a = 10;
let b = 20;
let result = a + b;
console.log(result); // 30
fetch('URL_HERE')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
resolved (सफल), rejected (असफल), या pending (प्रतीक्षित)।let resultFromServer = fetch('URL_HERE');
console.log(resultFromServer); // प्रॉमिस की स्टेटस: pending
async और awaitasync फंक्शन को असिंक्रोनस टास्क को नियंत्रित करने के लिए उपयोग किया जाता है।await शब्द एक असिंक्रोनस टास्क के परिणाम की प्रतीक्षा करने के लिए उपयोग किया जाता है।async function getData() {
let resultFromServer = await fetch('URL_HERE').then(response => response.json());
console.log(resultFromServer);
}
getData();
.then(): जब प्रॉमिस सफल होता है तो क्या करना है।.catch(): जब प्रॉमिस असफल होता है तो क्या करना है।.finally(): प्रॉमिस के परिणाम की स्थिति चाहे जो भी हो, उसे अंत में क्या करना है।fetch('URL_HERE')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error))
.finally(() => console.log('Operation finished'));
async/await का उपयोग हमें कोड को अधिक संरचित और स्पष्ट बनाने में मदद करता है।