Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

Fetch weather data from open weather

The code above describes step by step the procedure for fetching the weather forecast data for cities. 

In line number 10 we make a fetch request to an external api. We use the url address we have defined in line 7. This url must be known from before, in order to know what to use, and how to use it. In addition we pass 2 parameters inside this query string, the first is the city that we are going to fetch weather forecast information for. In this case we have hardcoded defined that this city is going to be 'Milan' (line 5),  the other one is the authentication token we have already acquired from the official website of the application.

Some API's require authentication tokens, or API keys, that you can require by registering or creating an account to the websites of these API's. Now i have already done it, i have required my Authentication token and pasted it in a variable in line 3. Both are used in line 7 if you watch carefully, especially at the end of the url string.

Then everything follows the normal procedure. Fetch returns a promise of the response object in line 10. When the response is back, we can go further. But the data is JSON data, so in order to work with, after we have received the response (console it if you want), we must parse it and make it again a JS object. Thus we use inside the callback function, the .json() function that returns also a promise! 

After the latest promise is also resolved, and the weatherResponse has been transformed from JSON object into a JS Object,  we can go further, with our normal weather data as always. This happens from line 17 and after.

Highly recommended to read, how the fetch API works here.