AJAX Image

AJAX Series: Async vs Sync

For the longest time I wanted to learn what AJAX was all about (you can interact with a website’s features without the need of reloading the entire website). The first thing that got me confused was the term “asynchronous”; what’s that about, right?

Well, there are two ways in which data is processed: the synchronous (or in “sync”) and the asynchronous (not in sync) way. To give you a better idea on how these two differ I highly suggest you to go over at NodeSchool.io and check out the learnyounode workshopper. If you are interested in learning Node.js or IO.js (NodeJS fork) I highly encourage you to take all the workshoppers that are available! These are excellent resources in which you will learn a lot about JavaScript.

Now, lets talk about sync programming. Usually, when we are developing, specially in Javascript, our scripts follow a certain pattern: we define global variables, objects, methods, scopes, etc. All of them follow a “recipe style” process. What I’m trying to say is that your script will wait until Step 1 is done in order to perform Step 2 and so on; this is the nature of sync programming and as you can imagine, it has its limitations since we need to wait until a certain part of the program has finished in order to proceed to the next step. This has a huge impact on performance and creates a bad user experience; which is no bueno! And for that reason (sometimes) it’s better to use an async approach.

Now, AJAX is all about asynchronous programming, which means, the client doesn’t have to wait for a server response in order to execute/process another part of the Web application. Remember, with synchronous programming the app follows a recipe style format; perform step 1, then 2, and so on. With an async approach, there isn’t such thing as a recipe style, which means, it doesn’t have to wait for step 1 to finish in order to execute step 2 or 3. This sounds great right? Well, it does! It improves the user experience and performance!

A living example on how AJAX works is by looking at Google Maps website. As you move your cursor to different parts of the page, there are certain tiles/squares that get updated with the new satellite pictures (these are XMLHttpRequest calls to the server). Something very important to take into consideration, is the fact that each tile doesn’t wait for the first one to finish loading and then proceed to the next one and so on, they don’t follow a particular order. That’s why certain tiles load faster than others.

I truly hope this little introduction gave you a better understanding on how AJAX works. This is Part I of my AJAX series. Make sure to come back next week (or subscribe to my newsletter!) for Part II where we will start to write some code!

Cheers 🙂