如何在提取API时设置超时时间?React Native - 提取时的超时时间
我想要的只是试图抓取数据10秒钟,如果失败,那么我想从AsyncStorage加载数据(以前保存并且每次更新时都会更新)。
大概我怎么做这不是正确的方式,我有点在编程xD noob但是这段代码在模拟器上工作得很好,但不能在我的手机(android)上工作。 AsyncStorage似乎不起作用。
这里是我的代码:
constructor(){
super()
this.state = {
fetching: false,
data: []
}
}
componentWillMount(){
this.setState({ fetching: true })
fetch('http://192.168.1.122:3000/categories.json')
.then(res => res.json())
.then(res => {
this.setState({
data: res,
fetching: false
})
})
.then(res => {
AsyncStorage.setItem(
'@Data:Monumentos',
JSON.stringify(res)
)
})
.catch(AsyncStorage.getItem(
'@Data:Monuments',
(err, dados) => {
if(err) {
console.error('Error loading monuments', err)
} else {
const monuments = JSON.parse(dados)
this.setState({
data: monuments
})
}
}
))
}
希望你能帮助我。谢谢!
[如何将setTimeout添加到使用redux的提取承诺?](https://stackoverflow.com/questions/38512238/how-to-add-a-settimeout-to-a-fetch- promise-that-uses-redux) –