Middle
/** * Potrebno je napisati funkciju koja prima URL kao ulaz, * izvršava GET zahtev na taj URL i vraća podatke (json). * Za dobijanje podataka koristi fetch. * Može se koristiti samo Promise API. * Ako dođe do greške tokom zahteva, pokušajte još 5 puta. * Ako na kraju ne uspe da dobije informacije, vrati grešku "Zadati URL nije dostupan". */ function get(url) { // kod ovde } get(url) .then(res => console.log(res)) .catch(err => console.error(err))
sobes.tech АИ
Одговор од АИ
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));