Why is javascript not asynchronous?

Learning to work in a single-threaded environment

Why is javascript not asynchronous?

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!

Why is javascript not asynchronous?

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.

Why is javascript not asynchronous?

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.

Why is javascript not asynchronous?

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:

Why is javascript not asynchronous?

Anouk is exhausted after reading all that, too.

Why is JavaScript not synchronous?

Well, Javascript is Synchronous, It is a synchronous, single-threaded language. Because it has a single thread that's why it can only execute one command at a time and the other commands need to wait for executing before the running command executes. And the term synchronous means one at a time.

Is JavaScript always asynchronous?

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.

Can we make JavaScript asynchronous?

Asynchronous JavaScript: Asynchronous code allows the program to be executed immediately where the synchronous code will block further execution of the remaining code until it finishes the current one.

Why JavaScript is single threaded?

JavaScript is a single-threaded language because while running code on a single thread, it can be really easy to implement as we don't have to deal with the complicated scenarios that arise in the multi-threaded environment like deadlock. Since, JavaScript is a single-threaded language, it is synchronous in nature.