Okay, so we’ve covered setTimeout( ). Let’s move on to AJAX.
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.