0

I have a chain of async functions that must return true or false, but i get undefined from a function than gets location.

here is the function that returns undefined:

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


i call it here:

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

THANKS!

B. Mohammad
  • 2,152
  • 1
  • 13
  • 28
  • You aren't returning anything in `getGeoLoc`. The return you have is in a callback function passed to `getCurrentPosition`, you need to return the result of `getCurrentPosition`. – Zaytri Dec 23 '19 at 17:15
  • thanks for help, i have added return to getGeoLoc but still returning undefined. I will edit the post accordingly – B. Mohammad Dec 23 '19 at 17:30
  • What does `getCurrentPosition` expect to be passed into it? – Zaytri Dec 23 '19 at 17:33
  • can you check this [reverse geocoding from LatLon](https://stackoverflow.com/questions/46590698/how-to-use-google-reverse-geocoding-with-react-native-expo) I hope its solution works for you – A.Risheh Dec 23 '19 at 19:37

2 Answers2

0

put your navigation call in 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
              },
divyang4481
  • 1,584
  • 16
  • 32
0

getCurrentPosition uses callbacks. If you want to have a separate function for receiving position then you can create a Promise like that

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

  });

And then you can call it with async/await this way:

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

    getLocation();
kenodek
  • 362
  • 1
  • 2
  • 13