Stili sinkron i fetch në JavaScript
Mund të punohet gjithashtu me fetch
në stilin sinkron:
button.addEventListener('click', async function() {
let response = await fetch('/ajax.html');
let text = await response.text();
console.log(text);
});
Rishkruaj kodin e mëposhtëm në stilin sinkron:
button.addEventListener('click', function() {
let promise = fetch('/ajax.html')
.then(
response => {
if (response.ok) {
return response.text();
} else {
throw new Error('status i keq i përgjigjes');
}
},
).then(
text => {
console.log(text);
}
).catch(
error => {
console.log(error);
}
);
});