Try Catch in JS, asking me to create Try statement even though i've already created one

Viewed 11

Hello StackOverflow Community,

I am working on a project where I am fetching data. If the fetch fails, I should alert the user of that.

Here is the example block of code:

async function showStackOverflow(){
try{
  fetch('urlexample', {
  method:'GET', 
  headers:
  {
    "Content-Type" : "///",
    'Authorization': `///`,
  }})
  
  .then(response => response.json())
  .then(data => console.log(data));
}
.catch((error) => {
  alert("Reauthenticate")
})
  };

When I create this block of code, it complains that I don't have a try statement. Any help appreciated.

0 Answers
Related