Sobes.tech
Intern

/* * You need to write a function that takes a URL as input, * asynchronously makes a GET request to this URL, and returns the data (JSON). * Use fetch to get the data. * Only Promise API can be used. * If an error occurs during the request, try 5 more times. * If it still fails, return the error "The specified URL is inaccessible". */

sobes.tech AI

Answer from AI

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('The specified URL is not accessible')); } }); }

// Usage: fetchWithRetry('https://example.com/data') .then(data => console.log(data)) .catch(error => console.error(error.message));


The `fetchWithRetry` function recursively attempts to fetch data using `fetch`. If the request fails or an error occurs, it retries up to 5 times. If all attempts fail, it returns an error with the message "The specified URL is not accessible".
/* * You need to write a function that takes a URL as… - sobes.tech