Sobes.tech
Middle

/** * Պահանջվում է գրել ֆունկցիա, որը ընդունում է URL որպես մուտք, * կատարում է GET հարցում այդ URL- ին և վերադարձնում տվյալները (json): * Տվյալները ստանալու համար օգտագործեք fetch: * Կարող եք օգտագործել միայն Promise API: * Եթե հարցման ընթացքում սխալ տեղի ունենա, փորձեք ևս 5 անգամ: * Եթե վերջում տվյալները ստանալ չհաջողվեց, վերադարձեք սխալ "Նշված URL հասանելի չէ": */ function get(url) { // կոդ այստեղ } get(url) .then(res => console.log(res)) .catch(err => console.error(err))

sobes.tech AI

Պատասխան 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));