Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 从异步函数react native获取未定义_React Native_Asynchronous_Async Await - Fatal编程技术网

React native 从异步函数react native获取未定义

React native 从异步函数react native获取未定义,react-native,asynchronous,async-await,React Native,Asynchronous,Async Await,我有一个异步函数链,它必须返回true或false,但我从函数中得到的是未定义的,而不是获取位置 以下是返回未定义的函数: async getGeoLoc(trigger = 'cDidMount') { return navigator.geolocation.getCurrentPosition( async position => { const isDataReady = await this.setCityFromCoordinate(

我有一个异步函数链,它必须返回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
    }
  },

谢谢承诺您的导航通话

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

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

    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/Wait调用它,方法如下:

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

    getLocation();

您没有在
getGeoLoc
中返回任何内容。您的返回值位于传递给
getCurrentPosition
的回调函数中,您需要返回
getCurrentPosition
的结果。感谢帮助,我已将返回值添加到getGeoLoc,但仍然返回未定义的值。我将根据
getCurrentPosition
编辑帖子。你能检查一下吗?我希望它的解决方案对你有用