Запіс на курсы па HTML, CSS, JavaScript, PHP, Python, фрэймворкам і CMS,
а таксама: дапамога ў пошуку працы і заказаў, стажыроўка на рэальных праектах→
⊗jsSpAXSS 260 of 294 menu

Сінхронны стыль fetch у JavaScript

Можна таксама працаваць з 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); } ); });
byenru