Asynchronous Javascript for Beginners (part 2)

Okay, so we’ve covered setTimeout( ). Let’s move on to AJAX.

AJAX (Asynchronous Javascript And XML) is where things get interesting. As you have have heard by now, the name AJAX is stupid. Most AJAX calls don’t actually use XML anymore, they use JSON. XML and JSON, by the way, are just different data exchange languages. Data exchange languages are different ways to format information and send it between different systems so that they can both understand it. So AJAX  is stupid, but to be fair though, we can all agree that it’s a way sexier name than AJAJ, so we’re sticking with it. I’ve heard the idea that you can think of the “X” as a variable for whatever data exchange language you want to use. I like that.

For now, we’re going to assume that we’re only using JSON. So AJAX now means ‘Asynchronous JS and JSON.’ Still not very descriptive. What does AJAX actually mean? Mainly, it just means getting JSON from another URL, or sending it. Boom. That’s mostly it, and I feel like that should have been conveyed somewhere in the acronym. It wasn’t, but it’s okay, we know now. We can take a deep breath and move on.

So the words in the AJAX acronym really just describe extra details about the main idea, mainly that this sending and receiving of JSON (or whatever) happens asynchronously. Take a look at this fiddle I found. It uses a JS method called fetch to do a bit of AJAX, namely a GET request. Change the code so that the JSON is logged to the console, then do like fifty console.log(“hello world)s or whatever you want right after the body of the fetch. As you can see, they all show up in the console first! Fetch is async, and as such it starts working, then stops for a while while the external server does its thing, letting the console.logs get to work until the fetch is ready to finish up and add its two cents to the console.

As you can now see, using AJAX means that async code become inevitable. In the next post, we will talk about the way async code has been dealt with traditionally, and the problems that arose as a result.

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s