Middle
/** * Nepieciešams uzrakstīt funkciju, kas pieņem URL kā ievadi, * veic GET pieprasījumu uz šo URL un atgriež datus (json). * Lai iegūtu datus, izmantojiet fetch. * Var izmantot tikai Promise API. * Ja pieprasījuma laikā notiek kļūda, mēģiniet vēl 5 reizes. * Ja beigās neizdodas iegūt informāciju, atgrieziet kļūdu "Norādītais URL nav pieejams". */ function get(url) { // kods šeit } get(url) .then(res => console.log(res)) .catch(err => console.error(err))
sobes.tech AI
Atbilde no AI
javascript function get(url) { const maxRetries = 5; let attempts = 0;
function tryFetch() {
return fetch(url)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.catch(error => {
if (attempts < maxRetries) {
attempts++;
return tryFetch();
} else {
return Promise.reject(new Error('Заданный URL недоступен'));
}
});
}
return tryFetch();
}
// Пример использования: // get('https://api.example.com/data') // .then(res => console.log(res)) // .catch(err => console.error(err));