【问题标题】:Exception propagation in React NativeReact Native 中的异常传播
【发布时间】: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


    【解决方案1】:

    Promise.reject() 似乎可以解决问题,但我仍然收到未处理的承诺拒绝。

    [warn][tid:com.facebook.react.JavaScript] Possible Unhandled Promise Rejection (id: 0):
    Location services disabled.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-02
      • 2021-07-02
      • 2021-04-22
      • 1970-01-01
      • 2015-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多