Created
January 31, 2021 11:51
-
-
Save timuckun/a418661da9d86f9a0292ff41795948c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
makeApiCall(url) { | |
return fetch(url) | |
.then((response) => { | |
if (!response.ok) { | |
throw new Error("Network response was not ok"); | |
} | |
return response.json(); | |
}) | |
.then(function (data) { | |
return data.observations[0]; | |
}) | |
.catch((error) => { | |
throw new Error( | |
"There has been a problem with your fetch operation:" + error | |
); | |
}); | |
} | |
fetchWeather() { | |
//fetch the observation | |
let url = | |
"https://api.weather.com/v2/pws/observations/current?stationId=" + | |
this.station + | |
"&format=json&units=" + | |
this.units + | |
"&apiKey=" + | |
this.api_key; | |
this.makeApiCall(url).then((observation) => { | |
// Here I want to set an instance variable | |
//this doesn't work. | |
this.observation=observation; | |
// weather data is defined as a const ourside the class | |
// this does work. | |
weather_data['observation']=observation; | |
return observation; | |
}); | |
// this is undefined | |
console.log(this.observation); | |
// this is OK | |
console.log(weather_data); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment