-1

I have a bunch of code that should return me coordinates considering an address. The documentation of this module gives this example:

Geocode.fromAddress("Eiffel Tower").then(
  response => {
    const { lat, lng } = response.results[0].geometry.location;
    console.log(lat, lng);
  },
  error => {
    console.error(error);
  }
);

which prints 2 coordinates.

Now, my code is done this way:

 const geoShopLocation = async () => {
      const location = await Geocode.fromAddress(product.addressLine1)
        .results[0].geometry.location
      return location
    }
    const shopLocation = geoShopLocation()
    console.log(shopLocation)

But at console.log level, I get this output, which I presume it's the full promise:

Promise {
  "_U": 0,
  "_V": 0,
  "_W": null,
  "_X": null,
}

I am trying every possible solution for this but I always get a promise back or "undefined". Can someone give me a hint for this?

Edoardo Trotta
  • 107
  • 3
  • 12
  • Duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Guy Incognito Dec 09 '20 at 10:05

1 Answers1

0

Async functions return Promise by default. Hence, you should either use await while calling geoShopLocation, or:

const geoShopLocation = async () => {
  const location = await Geocode.fromAddress(product.addressLine1)
    .results[0].geometry.location
  return location
}
geoShopLocation().then(shopLocation => console.log(shopLocation));
Onur Arı
  • 502
  • 1
  • 4
  • 16