I'm aware there's lots of similar questions but all the answers say use an async function with an await function call inside...which I'm already doing. After hours of banging my head off this, I need to ask the question...Feel free to down vote if you feel you have to, but I HAVE looked for an answer.
So I'm a little puzzled and could do with an extra set eyes on this. I'm using Axios to return data from an API call. I've got an async function sending the request with and await on the axios.get function request. So to my mind it should return data not a promise. However it's returning a promise and nothing that I've tried will return the data. I can console log this and it spits out the data, but the actual return is a promise object. If anyone could tell me what's wrong here I'd be grateful.
import axios from "axios";
import { LatLngTuple } from "leaflet";
const primaryKey = '#################################################';
const getGeoData = (apiUrl: string) => (
axios.get(apiUrl, {
timeout: 5000,
headers: {
"Content-Type": "application/json",
},
})
);
async function geoLocate(longAddress, country){
let geocodeUrlTemplate = 'https://atlas.microsoft.com/search/address/json?api-version=1.0&subscription-key={Azure-Maps-Primary-Subscription-key}&query={longAddress}countrySet={country}&view=Auto';
let geoRequestUrl = geocodeUrlTemplate.replace('{Azure-Maps-Primary-Subscription-key}', primaryKey)
.replace('{longAddress}', encodeURIComponent(longAddress))
.replace('{country}', country);
try {
const {data} = await getGeoData(geoRequestUrl);
const geoData = data.results[0].position;
return [geoData.lat, geoData.lon];
} catch (err) {
console.error(err);
}
};
export default geoLocate;