Sobes.tech
Middle

/** * Szükséges írni egy függvényt, amely bemenetként egy URL-t kap, * végrehajt egy GET kérést erre az URL-re, és visszaadja az adatokat (json). * Az adatok lekéréséhez használja a fetch-et. * Csak a Promise API-t szabad használni. * Ha kérés közben hiba történik, próbálja meg még 5 alkalommal. * Ha végül nem sikerül az információt megszerezni, adja vissza a "A megadott URL nem elérhető" hibát. */ function get(url) { // kód ide } get(url) .then(res => console.log(res)) .catch(err => console.error(err))

sobes.tech MI

Válasz az MI-től

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));