Middle
/** * É necessário escrever uma função que receba uma URL como entrada, * faça uma requisição GET a essa URL e retorne os dados (json). * Para obter os dados, usar fetch. * Pode usar apenas a API Promise. * Se ocorrer um erro durante a requisição, tentar mais 5 vezes. * Se no final não for possível obter as informações, retornar o erro "URL especificada não acessível". */ function get(url) { // código aqui } get(url) .then(res => console.log(res)) .catch(err => console.error(err))
sobes.tech IA
Resposta da IA
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));