JavaScript တွင် Synchronous Style Fetch
fetch ကို
synchronous style နဲ့လည်း အလုပ်လုပ်နိုင်ပါတယ်။
button.addEventListener('click', async function() {
let response = await fetch('/ajax.html');
let text = await response.text();
console.log(text);
});
အောက်ပါကုဒ်ကို synchronous style နဲ့ ပြန်ရေးပါ။
button.addEventListener('click', function() {
let promise = fetch('/ajax.html')
.then(
response => {
if (response.ok) {
return response.text();
} else {
throw new Error('အဖြေရဲ့ status မကောင်းပါ');
}
},
).then(
text => {
console.log(text);
}
).catch(
error => {
console.log(error);
}
);
});