Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Example of non blocking programming in node

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 160
    Comment on it

    Here we will see how the non blocking programming works and how the control goes from one program to another without sequence.

    Example for Non-Blocking:

    Let us create a text file file_2.txt. Now write some content in the file_2.txt

    Let us write the below content:

    Content for non-blocking example:The runtime environment interprets JavaScript using Google's V8 JavaScript engine.

    Now create a js file demo.js and write the following code:

    var filesys_nb = require("fs"); /*importing the file system module*/
    
    filesys_nb.readFile('file_2.txt', function (error, output) {
        if (err) return console.error(error);
        console.log(output.toString());
    });
    
    console.log("This is the end of the no blocking code.");

    Now execute the following command

    username@machinename:~$ cd path of the file
    username@machinename: path of the file$ node demo.js

    The above example explains the concept of non-blocking calls, here the program does not wait for reading file_2.txt and it just forwards the control to the code console.log("This is the end of the no blocking code."); and simultaneously continues reading the file_2.txt without blocking. The execution of Non-blocking programs is not sequential,

    Output:

    Executed first: This is the end of the no blocking code.

    Executed second:Content for non-blocking example:The runtime environment interprets JavaScript using Google's V8 JavaScript engine.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: