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. Experinced sync programmers often have more trouble with the concept than programming newbs. But don't be discourage, once you get it down it's back to familar tasks such as queries, loops and conditions.

NodeJS is server-side JavaScript. If you are familar with Javascript, you are probably aware of it's 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 Async?

Async and sync programming both have advantages and disadvantages. One is not better than the other, but for many jobs asynchronous code will be the more efficent choice.

Evaluating regular JavaScript expressions is very quick. Other tasks such as reading a file or quering the database, take much longer. If your application is calling to the database, it can complete many other tasks in the time it takes to recieve a response.

What's the Catch?

There is no 'catch', but asynchronous developers do need to consider factors that are irrelvant 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 recieved 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 handel Node's async nature.

Color Selector



Hex:


RBG:






Questions? Contact me on my social media:

YouTube

GitHub

Facebook

Instagram

Twitter

T-shirt Shop

Previous Lesson | Next Lesson