Thursday , 17 August 2017
Home / Programming / JavaScript / Node.js / Asynchronous – Node.js Tutorial for Beginners
Asynchronous
Asynchronous

Asynchronous – Node.js Tutorial for Beginners

Before we continue onto our blog web site. I would like to talk about asynchronous programming. You’ve probably heard this term before. You’ve probably heard of it’s opposite term which is synchronous programming. These 2 concepts are very important to understand before you go further into Node.js development.

Asynchronous

Let’s take a look at some JQuery code to see an example of Asynchronous programming.

The code above will simply make an AJAX POST request to a page called contact. Once the request is finished, then an anonymous function will be executed. If we were to run this script you’ll find that the second console message is fired first. Why?

Well, JavaScript can see that the following AJAX operation will take a while. So, it’ll execute any code after our POST request. When our operation is complete, then it’ll just run the function passed in earlier. This what’s called the event loop.

The event loop is what makes node powerful. You can have multiple operations running, but they won’t block each other. Blocking is when one piece of code has to fully run before the next task can run. Basically, Node.JS can multitask with asynchronous programming. You can query a database, upload a file, and start rendering a page all at once! This makes Node.js run faster than a lot of non-node websites.

Synchronous

Let’s create a fake synchronous version of our asynchronous JQuery example.

In the above example we have the same thing happening. We’re making an AJAX POST request to the contact page, but this time the console messages won’t be outputted UNTIL the contact page responses with some data. This means that if there are any other operations we would like to run won’t happen until POST request is finished.

Why Asynchronous Programming?

Synchronous programming works just fine for languages like PHP, but not for JavaScript. JavaScript has always been an asynchronous programming language for various reasons. Let’s say you had a slider powered by JavaScript and a button that will make an AJAX POST request.

If JavaScript was written synchronously, then our slider would stop running until our POST request was finished. By having asynchronous programming, then we can have multiple events running in the browser without interruption.

Node.js has adopted this style of programming for a couple of reasons. One of the big downfalls of Node.js is that it uses 1 core to run a single application. Meaning only 1 task can run at a time. The solution? Make everything asynchronous. By having code asynchronous, you can have multiple operations running along side each other without 1 blocking the other. Of course, this can be a bit tricky at times because you can end up having spaghetti code.

Even worse, you’ll experience something called callback hell. Which is just a term for nested functions. Don’t worry though, the Node.js community has flow control modules which will make code readable. We’ll take a look at flow control in a future tutorial.

A lot of people find asynchronous programming intimidating, but you’ll get use to it with practice. We’ll be doing a lot of asynchronous programming throughout this series.

Conclusion

Asynchronous is an important concept to understand as you’ll need to adapt to it when using Node.js. If you don’t understand it completely, then don’t worry. You’ll get a lot of practice. You can learn more about asynchronous by clicking here!

About Jasko Koyn

Check Also

Display Blog Posts

Displaying Blog Posts – Node.js Tutorial for Beginners

In this tutorial, we’ll be displaying blog posts to our visitors. To do this, we …

Leave a Reply

Your email address will not be published. Required fields are marked *

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link