admin管理员组文章数量:1389783
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?
- 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
1 Answer
Reset to default 4The 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
};
})
本文标签: javascriptUsing Fetch response in a if statementStack Overflow
版权声明:本文标题:javascript - Using Fetch response in a if statement - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744674787a2619043.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论