जावास्क्रिप्ट में सिंक्रोनस फ़ेच स्टाइल
आप सिंक्रोनस स्टाइल में
fetch के साथ भी काम कर सकते हैं:
button.addEventListener('click', async function() {
let response = await fetch('/ajax.html');
let text = await response.text();
console.log(text);
});
निम्नलिखित कोड को सिंक्रोनस स्टाइल में फिर से लिखें:
button.addEventListener('click', function() {
let promise = fetch('/ajax.html')
.then(
response => {
if (response.ok) {
return response.text();
} else {
throw new Error('खराब प्रतिक्रिया स्टेटस');
}
},
).then(
text => {
console.log(text);
}
).catch(
error => {
console.log(error);
}
);
});