Learning to work in a single-threaded environment

Kait Hoehne
Feb 25, 2018 · 4 min read

As people, we like structure. We like categories, descriptions, and putting everything we know into tidy little boxes. This is why I found JavaScript so confusing at first. Is it a scripting or a programming language? Is it used on the front or back end?

The wonderful (read: awful) thing about JavaScript is that most of the time, it’s a little bit of both. JavaScript has evolved so much over the years that it’s difficult to categorize. Today, I’m going to dive into whether JavaScript is synchronous or asynchronous and what workflow looks like under the hood.


JavaScript is Synchronous

Spoiler: at its base, JavaScript is a synchronous, blocking, single-threaded language. That just means that only one operation can be in progress at a time. That’s not the entire story, though!

Image by Aleks Shineleva

What if you have to make an expensive database request? You don’t want to be twiddling your thumbs while PG and Postgres grab those 800 songs you need for your music library. Synchronous code makes a programmer’s life very difficult, so the JavaScript community developed some great workarounds.

When you hear folks say that JavaScript is an asynchronous language, what they mean is that you can manipulate JavaScript to behave in an asynchronous way. It’s not baked in, but it’s possible! Here are a few ways to make that happen:


Asynchronous Callbacks

The earliest and most straightforward solution to being stuck in the synchronous world is using asynchronous callbacks (think setTimeout()).

Let’s use a database request as an example: asynchronous callbacks allow you to invoke a callback function which sends a database request (and any other nested callbacks) off to your app, where it waits for a response from the database, freeing up the rest of your code to continue running.

Image by Aleks Shineleva

Once the database request completes, the results (and any other nested code) are sent to the queue and then processed through the event loop.

In the diagram here, you can see how this differs from the synchronous code. Function C, along with E, F and G are all sent off to the browser, queue and event loop.

If you want a wonderful, clear explanation of this process, check out this demonstration by Philip Roberts.

While this is a great solution, it leaves something to be desired. Since you can’t predict exactly when function C will resolve, you have to nest all dependent functions within it. This gets messy fast and leads to the infamous callback hell that no one wants to deal with. It was this environment that inspired the promise.


Promises

In order to deal with callback hell, libraries like Bluebird or Q allowed programmers to clean up their syntax and write code that operated asynchronously but looked synchronous. This resulted in code that was easier to read and faster to run.

Image by Aleks Shineleva

With a promise, instead of bundling all dependencies into one code block and sending the entire thing off to the browser, we’re able to separate them out.

We can send the asynchronous callback (Function C) to the browser and use .then() to hold all other dependencies (E, F, and G) aside, running them only once Function C returns and runs.

This allows us to code in a more modular, readable way, while still gaining the benefits of asynchronous programming.


Async/Await

Promises were fantastic — so fantastic, in fact, that ES6 brought them into the language as a standard. But using promises still left asynchronous code feeling slightly wonky, so we now have the beautiful and stunning Async/Await to help us out!

There are entire blog posts and (I’m sure) books written about Async/Await, so I’m not going to go into it in too much depth, but suffice it to say that Async/Await allows you to:

  • Continue using promises
  • Write asynchronous code that looks and feels synchronous
  • Cleans up your syntax and makes your code more human-readable

Here are some of my favorite blog posts and videos on Async/Await, if you’re looking for more reading:

Thanks for reading! Cat tax:

Anouk is exhausted after reading all that, too.

Better Programming

Advice for programmers.

Thanks to Gabby Josebachvili

Kait Hoehne

Written by

Loves code, cats and coffee. 🤓☕️ Web engineer @qz. Previously @mic, @hopperacademy & @ThirteenWNET. Proudly from WI.

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade