Skip to content

Instantly share code, notes, and snippets.

@Elshaman
Last active September 16, 2020 14:42
Show Gist options
  • Save Elshaman/be69467bf9c5d69d348cbe03a52c2b58 to your computer and use it in GitHub Desktop.
Save Elshaman/be69467bf9c5d69d348cbe03a52c2b58 to your computer and use it in GitHub Desktop.
asynchronous api request(Chapter two): Classic Stone-aged Javascript XMLHttpRequest , this time with arrow syntax
/**
* Cristian Buitrago´s Weather API
* Second Iteration
* Sample with XMLHttpRequest classic's with callbacks, this time with arrow syntax
* Execute with any web browser
* method: GET
* Architectural Style: Callback
* Calls openweathermap's API(openweathermap.com/current for info)
*/
get_weather_data = (url, success , fail) => {
let httpRequest = new XMLHttpRequest();
httpRequest.open('GET', url);
httpRequest.onload = () => {
if(httpRequest.status === 200){
success(httpRequest.responseText)
}else{
fail(httpRequest.status)
}
}
httpRequest.send();
}
success = (response) => {
const dataObj = JSON.parse(response);
console.log(dataObj.main.temp)
}
fail = (status) => console.log(status);
const apiKey = 'your_weather_map_api_key';
const url = `https://api.openweathermap.org/data/2.5/weather?id=3688685&APPID=${apiKey}`;
get_weather_data(url , success, fail);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment