Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Node.js Event loop

    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 666
    Comment on it

    The event loop in Node.js works or runs in a single thread. That shows the application developer the code he/she will write that will be checked on a single thread. There are n number of threads which node js uses itself through libuv. But we don't need to think of this because that event were handled by node.js itself.
            Node takes the approach that all I/O activities should be nonblocking. Every call in which we need I/O that will come under callback.That call also returns immediately. By this, we will get multiple I/O operation in parallel in our code. As soon as an I/O operation is completed it's callback will be pushed on the event loop. It will be executed as soon as all the other callbacks that were pushed on the event loop before it is executed.

    Example:

     

    var name='Krishna';
    var place='Dehradun';
    app.get('showData',function(req,res){
     res.send(name+ 'is from' + place);
    }
    console.log('Execute first statement ');
    console.log('Execute second statement');
    
    
    Output:
    Execute first statement
    Execute second statement
    Krishna is from Dehradun
    
    

     

    Here In the example first var name ='Krishna' go to the stack and execute and name is initialize in memory then var place ='Dehradun' go to the stack and execute and name is initialize in memory
    Then event loop will check that next is asynchronous call so event loop will put them into event queue and when all code will executed then event loop will check the stack and if it is empty then it will check if there is any asynchronous call in event queue  and put it on to stack and stack execute it.

    By this way event loop is working in node.js

 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: