Sobes.tech
Intern

/** * É necessário escrever uma função que receba uma URL como entrada, * faça uma requisição GET assíncrona 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 a informação, retornar o erro "URL especificada não acessível". */

sobes.tech IA

Resposta da IA

typescript function fetchWithRetry(url: string, retries = 5): Promise<any> { return fetch(url) .then(response => { if (!response.ok) { throw new Error('Network response was not ok'); } return response.json(); }) .catch(error => { if (retries > 0) { return fetchWithRetry(url, retries - 1); } else { return Promise.reject(new Error('Заданный URL недоступен')); } }); }

// Использование: fetchWithRetry('https://example.com/data') .then(data => console.log(data)) .catch(error => console.error(error.message));


Функция `fetchWithRetry` рекурсивно пытается получить данные с помощью `fetch`. Если запрос неуспешен или произошла ошибка, она повторяет попытку до 5 раз. Если все попытки неудачны, возвращает ошибку с сообщением "Заданный URL недоступен".