Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Event Driven Asynchronous Callback in Node

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 227
    Comment on it

    To understand the asynchronous callbacks concept in node.js let us see the below code and try to understand what is the concept:

    var output = database.query("select  * from employee");
    console.log("Learning the conept of Event driven asynchronous callbacks");
    /*This code is of the synchronous type.*/

    1- In the first line of the query, we are fetching all the records from the employee
    table.

    2- In the second line we console.log("Learning the conept of Event driven asynchronous callbacks") on the console

    let's us say that the table consists of may records and the query is slow hence it takes extra seconds to get the resultset.

    As per the above code, JavaScript interpreter of Node.js will read the result set first and then it will run the console.log("Learning the conept of Event driven asynchronous callbacks"). Hence the user will have to wait for long to upload the page.

    If we see the execution model of PHP,If one of the requests turns in the slow execution of code, it turns to a slow page load for the particular user, but other users who have requested for other pages will not be affected.

    But if talk about the Node.js execution model - there is only a single process and if query slow down this process due to large data , this will affect the whole process and everything will stay still until the slow query process is completed.

    To avoid the above scenario, Node.js introduced the concept of Event Driven, Asynchronous callback, by making use of an event loop.

    To understand the Event Driven Asynchronous callback concept let us take the above query that fetches all the records from an employee table:

    database.query("select * from employee", function(record) {
    var resultset = record;
    });
    console.log(Learning the concept of Event driven asynchronous callbacks);
    /*This code is of asynchronous type.*/

    In the above code, instead of returning the result set directly from database.query(), we pass the function “query()” a second parameter as an anonymous function. In the previous code it is of synchronous type where database.query() method, will first return the result set, and then execute the next line code.

    But now Node.js will asynchronously handle database request, provided database.query() is part of an asynchronous library. Now it takes the query and sends it to database but it do not wait for the result set, instead when in near future the database server returns the output, then the anonymous function is executed that was passed to “database.query()”

    In the mean while it immediately executes console.log(), and later it will enter the event loop. Node.js continuously cycles through this event loop again and again whenever there is nothing else to do,waiting for events. Events such as querying the database and the query delivers late due to slow process.

 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: