0

I am having a hard time with javascript promises as a learner. I am building a small react app where you can type in a location and via basic api handling, get the weather.

However, as a twist I am trying to incorporate the user's current location by default. However the issue I am facing is that I can't get the data out of the promise object.

    const currentLocation = new Promise((resolve, reject) => {
        if(!window.navigator.geolocation) reject('User disabled location checking')
        else {
            window.navigator.geolocation.getCurrentPosition((position) => {
                resolve(`${position.coords.latitude},${position.coords.longitude}`)})}
    })

When I keep my location on, the promiseResult comes out as intended, but when I try to pass const currentLocation somewhere, it is passing in the whole promise object and not just the result. What am I missing here?

imbes
  • 71
  • 1
  • 6
  • Seems to be a duplicate of [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Samathingamajig Apr 04 '22 at 14:59
  • "I can't get the data out of the promise object" yes, this is what trips most people up with Promises. You **can't** unwrap a Promise, because there may not be anything to unwrap yet! What you **can** do is say "here, schedule this to run when there's something there" which is `.then`. Using `async/await` does the same thing under the hood. – Jared Smith Apr 04 '22 at 15:04

0 Answers0