This is the sample code:
// Use zipURL to receive long and lat by using zipcode
const userInfo = document.getElementById('userInfo');
const generate = document.getElementById('generate');
// Function to get the coords using the zipcode entered in the form
const getCoords = async (zipURL, zip, key) => {
// res equals to the result of fetch function
const res = await fetch(`${zipURL}?zip=${zip}&appid=${key}`);
try {
// data equals to the result of fetch function
const coords = await res.json();
return coords;
}
catch (error) {
console.log("error", error);
}
};
// Function to get the weather data using the lat and long
const getWeatherData = async (weatherURL, coords, key) => {
const res = await fetch(`${weatherURL}?lat=${coords.lat}&long=${coords.long}&appid=${key}`);
try {
const data = await res.json();
return data;
}
catch (error) {
console.log("error", error);
}
}
generate.addEventListener("click", (e) => {
e.preventDefault();
const zip = document.getElementById("zip").value;
const feelings = document.getElementById("feelings").value;
if (zip !== "") {
generate.classList.remove("invalid");
getCoords(zipURL, zip, key)
.then(getWeatherData(weatherURL, coords, key))
}
})
I want to receive the coords which are the longitude and latitude present in the promis object returned by the getCoords function. Then I want to use them in the getWeatherData function. But I can't seem to figure the small details. Please help me solve this as I have been banging my head against the wall for this to work!