Sobes.tech
Middle

/** * Je potrebné napísať funkciu, ktorá prijíma URL ako vstup, * vykoná GET požiadavku na tento URL a vráti údaje (json). * Na získanie údajov použite fetch. * Môžete použiť iba Promise API. * Ak počas požiadavky dôjde k chybe, skúste to ešte 5-krát. * Ak nakoniec nie je možné získať informácie, vráťte chybu "Zadaný URL nie je dostupný". */ function get(url) { // kód tu } get(url) .then(res => console.log(res)) .catch(err => console.error(err))

sobes.tech AI

Odpoveď od 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));