Help with weather app

Hi!

Can’t figure out why my getweather function does not work, mainly why the url does not work, seems like the variable location would not have a value but why, it clearly has a value.

I also tried to add a return value (location) to my getlocation function and use that to make sure that location has a value in getweather function but that did not help.

here’s my pen: http://codepen.io/Toheikki/pen/RKmKzx?editors=1010

working fine for me … shows … clouds … 9.0 celcius
in fact i commented out getlocation(); … dont need it as you are using it in getweather function … i just copied this line of code from it … $("#location").html(location); and added it to the getweather() function and put it under this line location = json.city + “,” + json.country;

out put for me is
Kildare,Ireland
Clouds
9.0 celsius

yes I was able to get it working but as you noticed I had to make it so that getlocation() is useless, just dont understand why the value that getlocation() inserts to variable location does not seem to “stay in”. I mean that even though getlocation() sets the correct value and displays it after that when getweather() uses the same variable location it is empty.

when you call
getlocation();
get-weather();
it works like this
getlocation(); … runs and takes certain amount of time to get info … (looks real fast to us)
get-weather();; … this runs … but as getlocation has not received the data yet … this is running without the required info and fails to do its job
getlocation(); … now finished has retrieved the data … but get-weather();has already run

this is why you run getlocation(); … and call get-weather(); from inside the getlocation function…
getweather() is then only called after the required info comes back from getlocation.

ok, that makes sense. Thank a lot! got to keep this in mind, I just assumed that getlocation has to finish before getweather is started. I haven’t come across this previously in Java and Scala programming courses, maybe they work differently…

Heres a repl … using settimeout to show how code runs … you will see how even though something is called first it could be last result back
It uses setTimeout function … kind of like how requests to api work only slowed down so you can see
also it uses promises so you can get a feel for how they work if your able to figure out whats going on