DEV Community

Afolayan Ademola Segun
Afolayan Ademola Segun

Posted on

Nodejs, Blocking Vs Non_Blocking

Blocking refers to operations that block further execution until that operation finishes while non-blocking refers to code that doesn't block execution. Or as Node. js docs puts it, blocking is when the execution of additional JavaScript in the Node. js process must wait until a non-JavaScript operation completes

Synchronous vs Asynchronous
Synchronous (or sync) execution usually refers to code executing in sequence. In sync programming, the program is executed line by line, one line at a time. Each time a function is called, the program execution waits until that function returns before continuing to the next line of code.

Asynchronous (or async) execution refers to execution that doesn’t run in the sequence it appears in the code. In async programming the program doesn’t wait for the task to complete and can move on to the next task.

I will give this simple illustration so that you will understand this two subject so well. Imagine you visit a restaurant and you order a black coffee, the now the waiter represent the server, he will get your orders but since black coffee isn't yet available, it will just be prepared, the waiter wont wait until they chef finish making your black coffee, he will move to another customer to accept his request or order, keeping in and that you made a request for black coffee and you haven't gotten any yet.

That is how it works. there is a reminder called call back function in Node, that tells the waiter/server that the coffee is finally ready and the customer which is you can finally get your coffee. This is how it works. All of this process is what is called Asynchronous. but when other customer have to wait until your request is being handled completely before they can be serve that is what is called synchronous.

Relating it to node, the waiter is the sever, the customers are the clients/users making requests. and Node.js gives avenue to serve asynchronously without blocking, that is what is called non-blocking. Thank you for ordering our coffee, I hope you enjoyed your coffee...

Discussion (0)