I'm building a time and weather app. I have two functions. showtime() that awaits getWeather(). The constants log fine in getWeather(), but when I try to retrieve the same constant in showtime() I get undefined... Fiddle.
<div class="weekday"></div>
const weekday = document.querySelector('.weekday');
const days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thur','Fri', 'Sat', 'Sun'];
setInterval(() => {
const d = new Date();
const day = days[d.getDay()];
async function showtime() {
await getWeather();
console.log(sunriseTime); // returns undefined
console.log(day) // logs fine, returns Wednesday.
weekday.innerHTML = day + ', Sunrise at: ' + sunriseTime;
}
showtime();
}, 1000);
//
const openweather_api = "https://api.openweathermap.org/exampleexample";
async function getWeather() {
console.log('run weather function first')
const response = await fetch(openweather_api);
const json = await response.json();
const sunriseTime = `${json.sys.sunrise}` // logs sunrise time in seconds.
}
getWeather(); // runs fine and retrieves values from Open Weather Json.