Weather app - issues: How can I manage async js?

EDIT: ok, I guess the main problem was that my Obj can’t read the porperty I need because they don’t exist yet, so it record an “undefined”.
This happens because JS works in an async way… but how can I manage it?

I’m working on the weather app on codepen. Here’s my pen

I’m trying to get current coordinates in order to use them for weather API. The rest of JS file is about a widget I found on thw weather API websites and that widget is no more executed once I added the navigation code above. So something get stuck, i guess.

Why can’t I get my coords in this example? Is there anything wrong in the code or is this something related to a codepen behaviour?
Thank you for your help!

  1. Maybe you’ve fixed things since posting, but it looks like it is working as you want it to now
  2. The “long answer”–but the best one–on understanding asynchronous behavior is to read this: https://github.com/getify/You-Dont-Know-JS/tree/master/async%20%26%20performance
  3. The short answer is that one of the easiest solutions is what you’ve done here, “nest” one function inside another. You’re actually forcing the process not to be asynchronous, because jQuery only calls the function provided to $.getJSON after the request comes back successful.
  4. Should you encounter a situation where too many functions get nested–“callback hell”–there are other solutions, including the ES6 “promises” and the modularization that http://callbackhell.com/ mentions. These aren’t necessary for this project (or, in the second case, possible on Codepen), but good to know about.
1 Like

Thank you so much!
Yes, I found a way to the solution but i supposed it wasn’t the best one, so I really appreciate your links :slight_smile:

I was just thinking that this kind of solution could be good for small purpose, like this one… but what in more complex projects? So thank you again! I’ll give a look and try to understand a bit how js works in “real life” :slight_smile: