Ads Top

Why NodeJS is asynchronous?

why nodejs is asynchronous? - the web stop

There are 2 ways of handling requests and executing them in programming:

Synchronous

 It waits for one operation to be completed before it handles and executes the second operation.

Asynchronous 

It does not wait for one operation to be completed, rather it executes all the operations in the one go.


JavaScript runs on the V8 engine and both JavaScript and V8 follow the synchronous way of handling operations. That's why NodeJS is here which follows the asynchronous way, eliminating the waiting time for the previous operation to be completed. For the same reason, it is also tagged as "Event Driven Non-Blocking I/O in V8 JavaScript". The Non-Blocking term is associated with NodeJS for its async behavior which does not block any other operations to happen. For the Event Driven part, let's dig a little deeper!



NodeJS

NodeJS is simply JavaScript written in C++. It runs on the V8 engine as JavaScript does but uses asynchronous programming.

What makes NodeJS asynchronous?

The first question which comes to the mind is how NodeJS is asynchronous when V8 and JavaScript follow a synchronous way of handling operations.

The answer is - Event Loop!

NodeJS has libUV which runs event loop periodically to check if there are events in the queue to be processed.

When a request has been sent to the OS (file opening, downloading a file etc.), it returns as an event to libUV. The event loop in libUV keeps checking the queue inside the libUV if something has happened. Whatever happens, it gets placed in the queue as an event. When an event finishes, libUV processes it and sends it to V8 as a callback.

At the same time, V8 is already running and is processing a code. Since it is synchronous in nature, it will let the current code finish executing first and then handles the callback.

Since the whole process of events coming from the OS, event loop in libUV checking the queue if an event is happening or done processing, V8 running the code synchronously and receiving the callback from libUV when an event is done processing happens inside NodeJS at the same time, it makes NodeJS asynchronous in nature.

This explains the Event-Driven programming in NodeJS. It simply means the programming which acts on events and whole paradigm of NodeJS is based on events.

4 comments:

  1. Thank you for sharing this information. This article is very interesting and useful. Keep up the good work!

    ReplyDelete
  2. Amazing web log and really fascinating stuff you bought here! I positively learned plenty from reading through a number of your earlier posts in addition and set to drop a discuss this one!

    ReplyDelete
  3. Nice blog. Thanks for sharing such great information.Inwizards Inc is a Nodejs Development company offers quality Node.js development services best in software industries. Intrested click here - Hire Nodejs Developers

    ReplyDelete
  4. I admire the valuable information you offer in your articles. I learn a lot of new things here
    Node JS Online training
    Node JS training in Hyderabad

    ReplyDelete

Powered by Blogger.