Medium
This question is a daily question and will count towards your daily streak.
The code above is fetching data from a URL using an asynchronous function with error handling. Assume the API occasionally returns an HTTP 404 error. Which statement best ensures the script gracefully handles the error and retries the fetch operation up to 3 times before throwing an error?
1async function fetchData(url) {2 if (!url) throw new Error('URL is required');3 const response = await fetch(url);4 if (!response.ok) throw new Error('Failed to fetch');5 return response.json();6}78(async () => {9 try {10 const data = await fetchData('https://api.example.com/data');11 console.log(data);12 } catch (error) {13 console.error(error.message);14 }15})();