Sobes.tech
Middle

/** * On vaja kirjutada funktsioon, mis võtab sisendina URL-i, * teeb selle URL-i GET-päringu ja tagastab andmed (json). * Andmete saamiseks kasuta fetch. * Võib kasutada ainult Promise API-d. * Kui päringu ajal tekib viga, proovi veel 5 korda. * Kui lõpuks ei õnnestu teavet saada, tagasta viga "Määratud URL-i ei ole saadaval". */ function get(url) { // kood siin } get(url) .then(res => console.log(res)) .catch(err => console.error(err))

sobes.tech AI

Vastus AI-lt

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