【发布时间】:2018-06-11 21:56:40
【问题描述】:
我正在使用 fetch API 访问休息服务器。如果服务器宕机,我需要设置 30 秒的超时并显示类似“发生超时”的警报。
我的 Fetch 调用
fetch('url', {
method: "POST",
headers: {
'Accept': '*/*',
'Content-Type': 'application/json',
},
body: JSON.stringify(objReq)
})
.then(response => response.text())
.then(responseJson => {
if (responseJson != null && responseJson != undefined) {
})
.catch(error => {
alert(error + " from error")
console.error(error);
});
我无法正确设置超时。是否有一种简化的方法来实现超时。关于在调用 API 之前检查互联网连接的任何建议。
【问题讨论】:
-
我想你正在寻找这个 - facebook.github.io/react-native/docs/netinfo.html#isconnected ,将返回连接状态
标签: javascript reactjs react-native ecmascript-6 promise