Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async function not returning on android

I'm having a problem with an async function not returning when running on android whereas it returns normally when run on iOS.

This is the function:

_getLocationAsync = async () => {
    let {status} = await Permissions.askAsync(Permissions.LOCATION);
    if (status !== 'granted') {
        this.setState({
            errorMessage: 'Permission to access location was denied',
        });
    }

    let location = await Location.getCurrentPositionAsync({});

    this.setState({location});
    return location;
};

and I'm using it in another function here:

async fetchMarkers(settings ){
    console.log("fetchMarkers");
    // console.log(settings);
    this.setState({listLoading: true});


    let location = await this._getLocationAsync();
    console.log("location is ", location);
    ....
    ....
}

This line is not returning in android, but it returns in ios. In android I tried logging the value of location just before returning it in _getLocationAsync and it logs a defined and correct object, I'm wondering why it's failing to return it then:

let location = await Location.getCurrentPositionAsync({});

I'm using React Native 0.53

like image 876
A. Wali Avatar asked May 07 '18 22:05

A. Wali


People also ask

Can an async function not return a value?

Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise.

Does async function return immediately?

With that design, you call the asynchronous function, passing in your callback function. The function returns immediately and calls your callback when the operation is finished. With a promise-based API, the asynchronous function starts the operation and returns a Promise object.

What happens when async method is not awaited?

The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't expected.

Can async function be callback?

Asynchronous callbacks are functions passed to another function that starts executing code in the background. Typically, when the code in the background finishes, the async callback function is called as a way of notifying and passing on data to the callback function that the background task is finished.


1 Answers

I think there are some reasons that Android can't get location.

I'm using this location option, anh it works well on android

// how accuracy should get location be
    const GET_LOCATION_OPTIONS = {
     enableHighAccuracy: false,
     timeout: 20000,
     maximumAge: 1000,
    };

    navigator.geolocation.getCurrentPosition(
      (position) => {
        const location = {
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
        };
        if (callback) { callback(location); }
      },
      (err) => {
        if (callback) { callback(null); }
      },
      GET_LOCATION_OPTIONS,
    );

Ref: https://facebook.github.io/react-native/docs/geolocation.html

like image 165
tuledev Avatar answered Oct 13 '22 02:10

tuledev