0

I have the following function:

func = async () => {
    let location = await getLocation();
    console.log(location)
}

it calls this function

getLocation = async () => {
    Geolocation.getCurrentPosition(
        (position) => {
            return 1
        }
    );
};

When I debug, I find that the code does indeed reach the return 1 line, but in func it always sets location to undefined. Am I doing something wrong with the asynchronous call here?

gkeenley
  • 2,920
  • 1
  • 18
  • 45
  • you have no return statements from `getLocation` - therefore undefined is returned - and since `Geolocation.getCurrentPosition` doesn't return a promise, there's no point in making `getLocation` an `async` function - note: `async/await` is syntax sugar for Promises - and your core function (`Geolocation.getCurrentPosition`) doesn't use Promises – Jaromanda X Jun 30 '20 at 02:00
  • simply change to `const getLocation = () => new Promise(resolve => Geolocation.getCurrentPosition(resolve))` ... note, `getLocation` is NOT, and does not need to be `async` – Jaromanda X Jun 30 '20 at 02:04
  • @JaromandaX This is the solution, thank you. If you want to put this as an answer, including that in `func()` you have to add `then()` to `getLocation()` I can accept it. – gkeenley Jun 30 '20 at 04:02

0 Answers0