Promises question
I have the following code:
This
geocode()
function is from the Google Maps API. I'm calling it here:
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
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?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 itsure