Why isn't this waiting until position.coords is returned let coords = await…
- User
Why isn't this waiting until position.coords is returned
let coords = await (navigator.geolocation.getCurrentPosition((position) => { console.log(position); return position.coords; }));
- Assistant
The
navigator.geolocation.getCurrentPositionfunction does not return a promise, which is why you cannot useawaitwith it directly. It works with callbacks instead. Theawaitkeyword can only be used with functions that return a promise.If you want to use
async/awaitwith the Geolocation API, you need to wrap thegetCurrentPositioncall in a promise. Here is an example of how you could do this:javascript