Home Linux HTML/CSS PHP PostgreSQL Node JS React VR Blog

NodeJS: Async VS Sync








Watch the Coding Commanders LAMP Animation:

Coding Commanders: Episode 1








JavaScriptasync vs sync

In order to learn NodeJS, you must first understand the concept of asynchronous programming and how it differs from synchronous code.

If you are an experienced synchronous programmer (i.e. PHP, Python), you may need some time to fully wrap your head around the concept. Experienced sync programmers often have more trouble with this concept than programming newbs. But don't be discouraged, once you get it down it's back to familiar tasks such as queries, loops, and conditions.

NodeJS is server-side JavaScript. If you are familiar with Javascript, you are probably aware of its asynchronous nature. However, if your experience is limited to independent features, you may not have fully utilized this important feature.


Synchronous Programming

Synchronous programming executes one line of code at a time. When the code is executed it requests the result, it will wait for a response with that result before executing the next line. (If the line of code read var x = 3 +4, the response back would be 7)

Click through the slide below to see this concept illustrated.

Asynchronous Programming

Asynchronous code doesn't wait. It will go line by line executing code, but does not wait for a response before executing the next statement.

Click through the slide below to see this concept illustrated.

Why Asynchronous?

Asynchronous and synchronous programming each have advantages and disadvantages. One is not better than the other, but for many jobs, asynchronous code will be the more efficient choice.

Evaluating regular JavaScript expressions is very quick. Other tasks such as reading a file or querying the database, take much more time. Your application can complete many other tasks in the time it takes to query a database. Asynchronous programming makes sure tasks only wait when they have to.

What's the Catch?

here is no 'catch', but asynchronous developers do need to consider factors that are irrelevant to synchronous programmers.

Start your future with a Data Analysis Certificate.

Click through the slide below to see this concept illustrated.

Some code is dependent on other code. For example, if res is the result of a database query and data is a function of res, the database response must be received before data can be calculated. Otherwise, your application will break!

When doing async coding, it is important to map out what functions are dependent on other functions.

Callbacks

NodeJS functions with callbacks are asynchronous. A callback is a function that is passed into another function. A function's callback is not executed until the host function is complete.

Callbacks are the classic approach used to deal with dependencies in JavaScript.

The next lesson is on callbacks. Now that you understand what asynchronous programming is, it is important to learn how to handle Node's asynchronous nature.

Color Selector



Hex:


RBG:






Questions? Contact me on my social media:

YouTube

GitHub

Facebook

Instagram

Twitter

T-shirt Shop

Previous Lesson | Next Lesson