1 回答

TA貢獻1810條經驗 獲得超4個贊
由于searchFunction是一個async函數,因此您需要使用 來調用它await。并且await只能在async函數內部調用,這就是為什么你需要創建callback一個async函數。
return另外,您需要從回調中執行
嘗試這個:
const geocode_res = await geocode(searchLocation, async ({ latitude, longitude }) => {
return await searchFunction(latitude, longitude)
})
您還需要從地理編碼函數返回。以便可以將返回值填充到geocode_res
async geocode(location, callback) {
try{
const GEO_URL = `https://api.mapbox.com/geocoding/v5/mapbox.places/${location}.json?access_token=${process.env.MAPBOX_API}`;
let results = await axios.get(GEO_URL)
const latLong = results.data.features[0].center
// add return here
return callback({
latitude: latLong[1],
longitude: latLong[0]
})
}catch(error){
console.log(` ${error}`)
}
},
添加回答
舉報