Skip to content Skip to sidebar Skip to footer

How Can I Make This Function Synchronous With Asyn/await

I'm trying to make this code to be synchronous but for some reason async/await doesn't work.. Im working in React-native with two differents modules. I want to see my geolocation i

Solution 1:

await / async do not stop code being asynchronous.

They are tools which let you write non-asynchronous style code by managing promises.

You can only await a promise. getLocalitation does not return a promise.

See How do I convert an existing callback API to promises? to get a promise for navigator.geolocation.getCurrentPosition.

Solution 2:

It is because you're using await keyword with a function that is not async

const resp = await getLocalitation();

You can either put an async before the () when defining getLocation or you can just remove await from const resp = await getLocalitation(), since you don't need to use await with something that does not return a promise.

In case you want to make getLocalitation async you do it like this

const getLocalitation = async () =>{
    console.log('DENTRO DE GetLocalitaion');

    const geoOptions={
        enableHighAccuracy: true,
        timeOut: 10000
    };

    const coordenates =  navigator.geolocation.getCurrentPosition( geoSucces,goFailure, geoOptions);
    console.log('DESPUES DE COORDENATES');

    return coordenates;
} 

Post a Comment for "How Can I Make This Function Synchronous With Asyn/await"