I'm checking users locations with HTML Geolocation before unlocking the front door. Geolocation takes a few seconds in addition to the time it takes the user to approve it, so currently I have a callback for what to do next.
function getLocationWithCallbacks(callback, error_callback) {
navigator.geolocation.getCurrentPosition(
function (position) {
let location = { "latitude": position.coords.latitude, "longitude": position.coords.longitude };
callback(location);
},
function (error) {
if (error.code === error.PERMISSION_DENIED)
error_callback()
}
)
}
And I call this function like so..
getLocationWithCallbacks(unlock_request, get_location_denied);
However, I'm not sure if there's a more logical way than this. It would be nice if events happened in a more linear fashion like ("pseudo code")...
function getLocation() {
navigator.geolocation.getCurrentPosition(
function(position) { return ['success', position] },
function(error) { return ['failure', error] }
}
And invoke it with...
let response = getLocation();
response[0] === 'success' ? unlock_request(response[1]) : get_location_denied(response[1])
However I believe this would involve adding promises and await statements which might make the original callback method more desirable.
I'm just not sure what's the cleanest, most logical way of implementing a getLocation function?