Skip to content

Instantly share code, notes, and snippets.

@timuckun
Created January 31, 2021 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timuckun/a418661da9d86f9a0292ff41795948c8 to your computer and use it in GitHub Desktop.
Save timuckun/a418661da9d86f9a0292ff41795948c8 to your computer and use it in GitHub Desktop.
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