This code is for a 'zillow' clone as a portfolio project.
My issue: I made a promise that retrieves the user's location using the geolocation API. I need this code to run asynchronously however, when the code executes, it resolves the promise before the user's location is found. The code works when I wrap certain parts of it in a timeout function but I really want to figure this out with using only promises. How do I fix this issue? Help is super appreciated!
P.S: I have not programmed my error handling yet, I wanted to get the above issue out of the way before doing so. Also, if you need any other files just let me know and I will upload them however, I think the issue is in either of the two files that are displayed below
Below is code from my 'controller.js' file:
const controlSearchCurrentLocation = async function () {
await findLocationView.getUserLocation(model.storeCurrentLocation);
/*
The setTimeout function is only used in order to give the
'findLocationView.getUserLocation(model.storeCurrentLocation)' time
to find and store the user's location
*/
setTimeout(() => {
currentCityResultsView.displayResults(
model.sendCurrentCityState,
model.storeData,
model.sendCoords,
model.sendHomesCurrentLocation,
model.sendHomesLength
);
}, 4000);
};
const init = function () {
controlSearchCurrentLocation();
controlSearch();
};
init();
Below is from my 'findLocationView.js' file:
class FindLocationView extends AppView {
getUserLocation(storeCurrentLocationFunc) {
return new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
function (position) {
this._success(position, storeCurrentLocationFunc);
}.bind(this),
this._failure,
this._options
);
if (navigator.geolocation) resolve("stored user location");
if (!navigator.geolocation) reject("could not store user location");
});
}
async _success(position, storeCurrentLocationFunc) {
let city, state;
const { latitude, longitude } = position.coords;
[...this._coordinates] = [latitude, longitude];
[this._latitude, this._longitude] = [latitude, longitude];
mapView.initMap(this._coordinates);
const data = await helpers.ajaxLocation(this._latitude, this._longitude);
city = data.results[0].address_components[2].long_name;
state = data.results[0].address_components[4].short_name;
storeCurrentLocationFunc(this._coordinates, city, state);
}
_failure(error) {
console.warn(`ERROR(${error.code}): ${error.message}`);
}
_options = {
enableHighAccuracy: true,
};
}
export default new FindLocationView();