Chain promises in Fetch

How can I return the response.ok and then make the console.log() and the window.location? is this a case that I should use another then?

function deleteSomething() {
  fetch(`https.api/delete.xyz`, {
    method: 'DELETE', 
    headers: {
      'Content-Type': 'application/json',
    },
  })
  .then(function (response) {
    if (response.ok) {
      return response.ok
      console.log("deleted!")
      window.location.href = `freecodecamp.org`
    } else {
      return Promise.reject(response)
    }
  })
  .catch(function (err) {
    console.log("There is an error", err)
  }); 
}
deleteSomething()

As soon as the return statement executes then anything after the return is pointless. You can move the console.log before the return, so that solves that problem. But you will have to make a decision here, either you want it to return response.ok or force the browser to load freecodecamp.org, but you can’t do both. If you really want the browser to load the new page here then I don’t see the point in trying to return a value because it will never get used in the first place.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.