Having trouble with the local weather app

Having trouble with the local weather app
0

#1

My Codepen - https://codepen.io/lisacope/pen/amXEwg

I just have some basic stuff so far - my real issue is the code logic. When I look at the console output, I am seeing the following -

going to do geolocation
console_runner-d0a557e5cb67f9cd9bbb9673355c7e8e.js:1 GEOLocation is true, at the top of the if statement
console_runner-d0a557e5cb67f9cd9bbb9673355c7e8e.js:1 right before json call =0 Lon = 0
console_runner-d0a557e5cb67f9cd9bbb9673355c7e8e.js:1 just before end of if statement
console_runner-d0a557e5cb67f9cd9bbb9673355c7e8e.js:1 after the if statement
console_runner-d0a557e5cb67f9cd9bbb9673355c7e8e.js:1 https://crossorigin.me/http://api.openweathermap.org/data/2.5/weather?lat=0&lon=0&appid=f27a663a728568282620c26af630aefb
console_runner-d0a557e5cb67f9cd9bbb9673355c7e8e.js:1 Just did getcurrentposition
console_runner-d0a557e5cb67f9cd9bbb9673355c7e8e.js:1 After GEOlocation =47.714812599999995 Lon = -122.20312639999999

it seems like it is jumping over the getcurrentposition call, the jumping over the json call, and then doing the json call, and then the getcurrentposition call! What am I doing wrong!!!


#2

Remember that AJAX calls are asynchronous.

That means that the browser dispatches the request for information then continues processing code while waiting for a response.

When the response comes in, then it executes a callback. That callback is where you can read/use/manipulate the response.

The geolocation API is asynchronous as well.

Right now your $.getJSON function is outside the geolocation callback. That means the request for geolocation will be triggered and the ajax requests will trigger and they are not guaranteed to finish in the same order (asynchronous).

You have to wait until you have a location then request the weather JSON.


#3

Thank you so much! Very helpful!


#4

You’re welcome! I’m glad I could help. :slight_smile: