I have a function that uses the geocoding api to turn a string(address/zip/city) into coordinates. I have another function that needs the coordinates returned from the geocoding api to call a different api that searches for events. However, I am new to async javascript and I am having trouble with async/await. The event api is saying that the coordinates that should return from the geocode api are undefined, but a few seconds later, the coordinates are returned. I need the event api to stop execution until the coordinates are returned from the geocoding api. Can somebody please point me in the right direction?
Here is the code for the two functions
function toLatLng(input) {
$.ajax({
type: "get",
url: GEOCODE_API_URL + input,
datatype: "json",
success: (data) => {
if(data.status === "OK") {
return new Promise((resolve, reject) => {
resolve(data.results[0].geometry.location);
});
}
else if(data.status === "ZERO_RESULTS") {
alert("The search yielded no results");
}
else {
alert("Unable to search for events");
}
},
error: (error) => {
console.log(error);
},
});
async function getEvents(input) {
let coordinates = await toLatLng(input);
let queryStr = `&latlong=${coordinates.lat},${coordinates.lng}`
$.ajax({
type: "get",
url: API_URL + queryStr,
datatype: "json",
success: (data) => {
console.log(data);
},
error: (err) => {
console.log(error);
},
});
}