JavaScript-ում fetch-ի սինխրոն ոճ
Կարելի է նաև աշխատել 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);
}
);
});