【发布时间】:2017-02-15 09:12:51
【问题描述】:
这是一个函数geolocate(),它在 React Native 中异步调用地理定位服务。
问题是一旦在geolocationError() 中抛出错误,它就不会传播到父geolocate(),而是立即出现红色错误屏幕。
如何将错误传播到geolocate() 的catch() 块?
async geolocate() {
try {
let result = await navigator.geolocation.getCurrentPosition(
this.geolocationSuccess.bind(this),
this.geolocationError.bind(this),
{enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
);
if (result != null) return result;
} catch(err) {
// This doesn't get called
Alert.alert(
"Location unknown",
"Turn localization services on.",
[
{text: 'OK', onPress: () => console.log('OK')},
]
);
};
}
geolocationError(err) {
console.log(err);
throw err; // Stops here -> Promise.reject() does better job here, but still results in "Unhandled promise rejection"
}
【问题讨论】:
标签: reactjs exception-handling react-native try-catch throw