Promises question

I have the following code:
function geocode(location) {
geocoder = new google.maps.Geocoder();

if (!location) {
console.error("Location argument is null.");
return;
}

return new Promise((resolve, reject) => {
geocoder.geocode({ address: location })
.then((result) => {
const { results } = result;

if (results) {
const coordinates = results[0].geometry.location;
const lat = coordinates.lat(); // These are functions
const lng = coordinates.lng();

resolve({ lat: lat, lng: lng });
}
})
.catch((error) => {
console.error("Geocode was not successful: " + error);
reject(error);
});
});
}
function geocode(location) {
geocoder = new google.maps.Geocoder();

if (!location) {
console.error("Location argument is null.");
return;
}

return new Promise((resolve, reject) => {
geocoder.geocode({ address: location })
.then((result) => {
const { results } = result;

if (results) {
const coordinates = results[0].geometry.location;
const lat = coordinates.lat(); // These are functions
const lng = coordinates.lng();

resolve({ lat: lat, lng: lng });
}
})
.catch((error) => {
console.error("Geocode was not successful: " + error);
reject(error);
});
});
}
This geocode() function is from the Google Maps API. I'm calling it here:
// Create new markers
locations.forEach(async (location) => {
const coordinates = await geocode(location);

if (coordinates) {
const marker = new AdvancedMarkerElement({
map: map,
position: coordinates,
title: "Uluru",
});
}
});
// Create new markers
locations.forEach(async (location) => {
const coordinates = await geocode(location);

if (coordinates) {
const marker = new AdvancedMarkerElement({
map: map,
position: coordinates,
title: "Uluru",
});
}
});
My question is, do I really need to wrap the geocode function in a Promise constructor? Admittedly, I got this answer from chatGPT after having difficulties with calling the data from geocode func inside the forEach. Whenever I tried to make a new marker, it wouldn't wait for the geocode func to return. My understanding of Promises is really limited -- but I did try to use the await syntax on the geocoder.geocode() line but my IDE complained that 'await' has no effect on the type of this expression -- so I'm assuming the way it's set up in the Google library doesn't allow you to use await?
3 Replies
glutonium
glutonium4w ago
i mean if the geocoder.geocode() returns a promise then why wouldn't await work? I don't seem to understand how it's applicability has anything to do with the library afterall async await is just a syntactic sugar built on top of Promise. So something working with Promise and not with asyn await doesn't really make much of a sense also about the fact that it doesn't wait for the func to retun within the forEach what if you wrap the whole location.forEach() code within an asyn IIEF? just out of curiosity, does that work?
vince
vince3w ago
Thanks for the response! I'll have to get back to you on this one First I'll try just await on the geocoder.geocode() func even though vscode flagged it
glutonium
glutonium3w ago
sure