FCC's Weather App Project Part Two: Bittersweet Callbacks

Upon gathering my data from Weather Underground, I had written a simple script calling each piece of data thinking that is all I would need. I was wrong, however, and the reason is due to JavaScript's advantage as well as its disadvantage as a programming language.

JavaScript executes programs in an asynchronous fashion, which is unusual to a Ruby programmer like me. What's the difference? A synchronous program is executed line by line and whenever a defined function is called, the computer waits until the function has completed its work to move execute the remaining code. Asynchronous languages execute code whether or not a function has completed its work or not. This is a little confusing, but it is one the main reasons JavaScript is so popular: its quick. So why would this cause programming problems?

If I were to simply assign the "$.getJSON" call to a variable and log it to the console, it would return the JSON history, but not be accessible. Due to the asynchronous nature of JavaScript, the function executes after the JSON data is no longer available. However, JavaScript has a unique property that is both unusual, but also extremely useful: passing functions into functions. In order to retain the data from the API call we have to assign the data to a function and app it into other functions that use the data for our purposes. Imagine if you will that you have predefined functions and you’re essentially calling back to them throughout your script. This is called a callback and it is quite confusing but also quick. Here’s an idea of what a callback looks like in my script:

function testIdea(value){
console.log(value);
var apiCall = "https://api.wunderground.com/api/f946ee8192f9f7ca/conditions/q/" + value.location.state + "/" + value.location.city + ".json";
callData(apiCall);
}


var geoLocate = $.getJSON("https://api.wunderground.com/api/f946ee8192f9f7ca/geolookup/q/autoip.json", function(api) {

testIdea(api);

})

This tends to be very confusing, but not too bad once you get the hang of it. However, this outlines one of the disadvantages JavaScript has for large scale apps versus another backend language like Java. Imagine having different sets of callbacks for multiple types of information. The script will get quite confusing. However, now my scripts are set up and all I have to do it make my site pretty and add some minor functionality. The hard work is over and its nothing but sunny skies ahead!

FCC's JavaScript Weather App: Introduction to API's

The Goal: To create an app that takes weather data from a weather information site and display it to a site so that any user can get their local weather data (in Celsius and Fahrenheit) from my app. Here's an example of my end goal.

As part of my goal to become really good at JavaScript and Java, I've started to take on projects on Free Code Camp again. This project required me to find a good API or Application Programming Interface. What is an Application Programming Interface? Interface is just a fancy word for a meeting ground of two organizations or systems. So an Application Programming Interface is a sort of meeting ground for one application to retrieve data from another application. This is important because, generally speaking, there are two ways to obtain data. One way landed me in trouble with a web admin and is incredibly slow and tedious and generally not allowed. That is to scrape the data off a website with a web crawler. The other is to use an API. Use API's. Its a skills employers need and its what developers have set up (with certain conditions such as a payment or providing credit to the data they have obtained) so that you can safely and securely obtain valuable information.

With JavaScript, an API returns an object in JSON form. Here is an example from my project's api call that was printed to the console.


 This is a pretty sweet deal and I must provide credit to the best weather API that is both simple and easy to use: Weather Underground. So how can we make this object useful to our application? The default language of data transmission on the web is JSON (which is why JavaScript is popular). This means that with an API call with jQuery (outlined next post) that I can simply pull data from this object with a command such as (object name).current_observation.UV. Any guess what data this would return? It would return the integer value of 3, where (object name) is the name of a local variable. This might be a little cloudy now, but I plan on outlining how this works exactly in my next post. Get it? Cloudy?! Weather??? Alright, till next time...

Aspirations

One of the key success factors with me landing my first software job, was blogging. This allowed me to do two things:

  1. Maintain accountability in my coding journey
  2. Write a blog as if I were teaching the code I was learning. (You remember more of what you teach)
As such, I think it is important to write out what I want to learn for the first two reasons as well as show my intended path. When I started my journey, my goal was to: "become fluent in Ruby and conversant in JavaScript." Now that I'm employed, my goals serve to first master my current role (automated tester) and secondly to explore what ultimate career path I want to take. I enjoy testing, but I am also very curious about becoming a developer. Luckily, the next two languages I'm delving into: JavaScript and Java are both useful in testing. However, I will be learning some web development skills and begin demonstrating that here.  I will say that any developer should be an excellent tester first. And anyone wishing to become a tester should also learn to become a great developer for the sake of proficiency in their career (imagine being able to point out code bugs upon finding errors).

My goals (in this order too) are as follows:
  1. Master my current field: automated testing
  2. Learn Node.js and complete a simple project with it
    • I am currently working through Manuel Kiessling's Node Beginner Book
    • I hope this will also serve my learn JavaScript goal
  3. Master the MVC framework
  4. Become proficient in Java (and possibly Java implementations of Ruby like JRuby and Groovy)
My interest, should I become any sort of developer, is to become a backend developer rather than a front end. I find this far more fascinating than the frontend and Node and Java are excellent skills to have for this reason. 

However, I will say the most important skills to learn to succeed in this field are the soft skills. Learn to speak in front of a group, learn to communicate and ask for help, and learn to be aggressive: give talks and network hard. This is a topic for next blog. See you then!