【发布时间】:2016-12-28 19:40:55
【问题描述】:
如果在我的 react-native 应用程序中获取期间互联网连接丢失,我会收到 Network request failed 并且应用程序崩溃。
updateClientData() {
var cachedData = null;
AsyncStorage.getItem('cachedData').then((cachedDataString) => {
cachedData = JSON.parse(cachedDataString);
})
.done(() => {
if (cachedData) {
const base64 = require('base-64');
return fetch('https://...data.json', {
method: 'get',
headers: {
'Authorization': 'Basic '+base64.encode("..."),
}
})
.then( (response) => {
// never called:
return response.json();
})
.catch( (error) => {
//Shouldn't this catch network errors? It never gets called.
console.log('caught network error');
})
.then( (responseJSON) => {
//do something with the JSON
})
}
});
},
我希望能够优雅地处理这个问题,而不是让它崩溃。有什么想法吗?
【问题讨论】:
-
如何将 catch 调用移到最底部而不是放在两个 then 调用之间?
标签: react-native fetch