Arte
Arte

Reputation: 417

Using Fetch response in a if statement

I have a fetch function that takes a URL and gives out the response.

public getHTTPResponse(URL : string) {
    fetch(URL).then(function(response) {
        return response;
    })
}

I try to use the response in a IF-statement.

For example if the response is 200 it should be true. So far I have:

if (this.getHTTPResponse(item.ListUrl) === )

Where item.ListUrl is a link. I can't get my head around on what I should have on the right side of the operator to test if the response is 200 (or any other for that matter). Any help?

Upvotes: 1

Views: 9553

Answers (1)

Stundji
Stundji

Reputation: 864

The response object has a property status which value is an integer. Try like that:

  fetch(URL)
   .then(function(response) {
      if(response.status === 200) {
         // do something
       };
     })

Upvotes: 4

Related Questions