我希望在函数结束之前运行一个回调函数。但是,回调似乎是异步的,我正在构建的数组最终是未定义的。下面是代码:
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 始终返回未定义。我已经检查以确保 google-maps-api 是正确的,并且我在其他地方使用了相同的功能(它工作正常)。有什么方法可以使回调同步或使其在 return 语句之前暂停?还是我需要实现某种异步功能?谢谢!