最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using Fetch response in a if statement - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Sep 9, 2019 at 14:18 Michael 3,2397 gold badges46 silver badges92 bronze badges asked Nov 18, 2018 at 9:17 ArteArte 4172 gold badges6 silver badges16 bronze badges 3
  • You can't do anything on the right hand side; getHTTPResponse doesn't currently return anything, and if it did paring promise equality rarely makes sense. – jonrsharpe Commented Nov 18, 2018 at 9:26
  • Possible duplicate of How to get data returned from fetch() promise? – jonrsharpe Commented Nov 18, 2018 at 9:27
  • Can you post your success and failure response here so we can see what type of response it's returning – Prashant Pimpale Commented Nov 18, 2018 at 9:58
Add a ment  | 

1 Answer 1

Reset to default 4

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
       };
     })
发布评论

评论列表(0)

  1. 暂无评论