Does the throw new Error always get executed in this code?

Consider the following code:

fetch(url, {
    method: 'POST',
    headers: {
      'Content-type': 'application/json',
      'apikey': apiKey
    },
    body: data
  }).then(response => {
    if (response.ok) {
      return response.json();
    }
    throw new Error('Request failed!');
  }, networkError => {
    console.log(networkError.message)
  })

Does the throw new Error part always get executed because no else statement is used? Or if it doesn’t get executed what will be the reason?

This is from the cheat sheet and a similar code is present in the lesson…

Thanks!!!

Consider what return does. What happens to control flow when return is executed?

2 Likes

Ok now I understood. Thanks :heart:

1 Like