Weather App - Changing a global from inside a function

Hi everyone,
In the WeatherApp task I create a global url str. Then I manipulate the str in the function but it doesn’t change it out of the function. I guess JS is designed so that u can’t change globals from inside a function, and the way to do it is by returning the variable. Am I correct?


globals are globals you change them anywhere and the change.

the problem you are having is that you are modifying it in a callback, so it is not actually modifying it until after you try using it. I’s suggest creating another function and calling it after you modify it with the new url.

Ok, I understand it is an asynchronous function.

  1. How can I read more about Asynchronous functions in JS?
  2. I don’t exactly understand your suggestion. In the JS you can see two JSON requests. The second one is dependent on the first one, and should only be executed after (the first one determines the URL the second one uses. How can I control the order of timing of execution?
    Here is the URL:

Thanks in advance,

Read sub-heading Dependence chain of AJAX requests for how to control the timing of ajax calls. The article discusses different ways to write ajax requests and is worth reading from beginning to end.
A youtube video discussing how asynchronous code works in JS. I would type the code examples from the video in your favorite browser console. The video may be hard to understand if your a beginner to programming because the speaker introduces some concepts not explored in fcc.

1 Like

Check out YDKJS.

Thanks everyone. Reggie01 - great video.
Practically I have 2 options for the Ajax JSON requests - 1. .getJSON and 2. .ajax . Is there any reason why to prefer either? More specificly, this article presents methods to chain and time control Ajax requests. Can I do the same with a getjson request?

Yes, you can do the same thing with $.getJSON request. $.getJSON and $.ajax return a promise that you can chain .then() or add .done().

This is how I chained my ajax requests.