Middle
/** * Es necesario escribir una función que tome una URL como entrada, * realice una solicitud GET a esa URL y devuelva los datos (json). * Para obtener los datos, usar fetch. * Solo se puede usar la API Promise. * Si ocurre un error durante la solicitud, intentar 5 veces más. * Si al final no se puede obtener la información, devolver el error "URL especificada no accesible". */ function get(url) { // código aquí } get(url) .then(res => console.log(res)) .catch(err => console.error(err))
sobes.tech AI
Respuesta de la 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));