I'm looking to run a callback function before the function ends. However, the callback seems to be asynchronous, and the array I'm building ends up as undefined. Here is the code below:
function getLocDetails(location) {
const google = window.google
const service = new google.maps.places.PlacesService(mapRef.current)
var request = {
placeId: location.place_id
}
function callback(place, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
// console.log(place)
return place
}
}
let tempDetailedPlace = service.getDetails(request, callback)
console.log(tempDetailedPlace)
return tempDetailedPlace
}
tempDetailedPlace always returns as undefined. I have checked to make sure the google-maps-api is correct, and I have used that same function elsewhere (it's working correctly). Is there any way to make the callback synchronous or make it pause right before the return statement? Or do I need to implement some kind of async function? Thanks!