【问题标题】:Getting undefined from async function react-native从异步函数 react-native 获取未定义
【发布时间】:2020-04-14 22:36:33
【问题描述】:

我有一个必须返回 true 或 false 的异步函数链,但我从函数中得到未定义而不是获取位置。

这里是返回未定义的函数:

async getGeoLoc(trigger = 'cDidMount') {
   return navigator.geolocation.getCurrentPosition(
      async position => {
        const isDataReady = await this.setCityFromCoordinate(
          trigger,
          position.coords.latitude,
          position.coords.longitude,
        );
        console.log('isDataReady getGeoLoc', isDataReady); // this gives true in console
        return isDataReady
          },


我在这里称呼它:

async getLocationPermission(trigger) {
    if (Platform.OS == 'android') {
      const response = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
      );
      if (
        response == PermissionsAndroid.RESULTS.DENIED ||
        response == PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN
      ) {
        Alert.alert(
          i18n.t('search:geolocation_disabled'),
          i18n.t('search:need_location'),
        );
        return false;
      } else {


        return await this.getGeoLoc(trigger);
      }
    } else {
      return await this.getGeoLoc(trigger);
      // for ios go directly here
    }
  },

谢谢!

【问题讨论】:

  • 您没有返回 getGeoLoc 中的任何内容。你得到的返回是在一个回调函数中传递给getCurrentPosition,你需要返回getCurrentPosition的结果。
  • 感谢您的帮助,我已将返回添加到 getGeoLoc 但仍返回未定义。我会相应地编辑帖子
  • getCurrentPosition 期望传入什么?
  • 你能检查一下这个reverse geocoding from LatLon我希望它的解决方案对你有用

标签: react-native asynchronous async-await


【解决方案1】:

将导航调用放入 Promise

  function  getGeoLoc(trigger = 'cDidMount') {
 return new Promise((resolve, reject) => {
       navigator.geolocation.getCurrentPosition(
          async position => {
            const isDataReady = await this.setCityFromCoordinate(
              trigger,
              position.coords.latitude,
              position.coords.longitude,
            );
            console.log('isDataReady getGeoLoc', isDataReady); // this gives true in console
            return isDataReady
              },

【讨论】:

    【解决方案2】:

    getCurrentPosition 使用回调。如果你想有一个单独的函数来接收位置,那么你可以创建一个这样的 Promise

        const myCoord = () =>
      new Promise((resolve, reject) => {
        const geoSuccess = position => resolve(position);
        const geoFailure = error => reject(error);
    
        navigator.geolocation.getCurrentPosition(
          geoSuccess,
          geoFailure,
          geoOptions
        );
    
        const geoOptions = {
          timeout: 5000,
          maximumAge: 5000,
          enableHighAccuracy: false
        };
    
      });
    

    然后你可以这样用 async/await 调用它:

    const getLocation = async () => {
          const response = await myCoord();
          console.log(response);
        };
    
        getLocation();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多