2016-10-06 8 views
0

ここには、React Nativeでジオロケーションサービスを非同期に呼び出す関数geolocate()があります。リアクションネイティブの例外伝播

geolocationError()の内部でエラーがスローされると、それは親のgeolocate()に伝播しませんが、赤のエラー画面は即座に表示されるという問題があります。

catch()ブロックにエラーを伝播させるにはgeolocate()

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" 
    } 

答えて

0

Promise.reject()は、トリックを行うようだが、私はまだ未処理の約束拒否を取得します。

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