score:0

there are two types of error that might defeat a fetch request, either the link doesn't exist, or a link exists but doesn't have data to be read. a error-catch block can be used to log the error without crashing the programme. i have used the following structure to fetch from a few thousand generated links where every now and then, one of the links is bad or the json data not present. it prevented crashes and reported silently to console.

fetch(`${url}`)
  .then(response => {
     if (!response.ok){throw new error ('bad response');} 
     else { return response.json()};
  })
  .then(data => { 
      // process data here or pass to processing function;    
      parsedata(data)
  })
  .catch(error => {
     // if in a loop can also log which url failed;
     console.log('error made: ', error);
  });

Related Query

More Query from same tag