Difference between Blocking Code and Non-Blocking Code in Node JS

create a file input.txt

create a file callback.js

Output when blocking code:

Coding 4 Developers originated for sharing running codes with programmers. The journey was started on 14th January 2016. Coding for developers is not a tutorial website where you can learn step by step. It has running codes, you just can download or copy paste codes for your use.

Program Ended


Output when non-blocking code:

Program ended before read file

Coding 4 Developers originated for sharing running codes with programmers. The journey was started on 14th January 2016. Coding for developers is not a tutorial website where you can learn step by step. It has running codes, you just can download or copy paste codes for your use.

 

These two examples explain the concept of blocking and non-blocking calls.

  • The first example shows that the program blocks until it reads the file and then only it proceeds to end the program.
  • The second example shows that the program does not wait for file reading and proceeds to print "Program ended before read file" and at the same time, the program without blocking continues reading the file.

Share This:

Leave a Reply

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