Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7905163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:22:22+00:00 2026-06-03T10:22:22+00:00

Have been following this fabulous tutorial . Being new to Javascript and functional programming,

  • 0

Have been following this fabulous tutorial. Being new to Javascript and functional programming, I wanted to understand what non-blocking essentially means. I intentionally added a “sleep” of 10 seconds in my JS code, to achieve blocking behavior.

    function route(pathname, handle)
{
    console.log("About to route a request for :"+pathname);

    if(typeof handle[pathname]==='function')
    {
        handle[pathname]();
    }
    else
    {
        console.log("No request handler for "+pathname);
    }


    sleep(10000);
    console.log("Exiting router");
}

function sleep(milliSeconds)
{
    var startTime = new Date().getTime(); // get the current time
    while (new Date().getTime() < startTime + milliSeconds); // hog cpu
}

exports.route=route;

This code is being used as a callback from another “server” script, which I am calling from a browser. I expected that once I fire simultaneous 100 requests to my server script, I would get parallel 100 responses after 10 seconds. But this code runs through the request one by one. This certainly fails the philosophy behind node.js right ?? This doesn’t even happen when I do such bad code in a Java servlet and run on Tomcat !

Another observation in this scenario, was that the requests were not handled chronologically – they are executed randomly. This doesn’t sound good to me !!

I believe there is some issue with my code – please help me understand the concepts here, with the answers to my 2 queries (the other one on chronology).

Thanks !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T10:22:24+00:00Added an answer on June 3, 2026 at 10:22 am

    I expected that once I fire simultaneous 100 requests to my server script, I would get parallel 100 responses after 10 seconds. But this code runs through the request one by one.

    Yes. Node is strictly single-threaded so each request will be run in serial. There is no parallelism in the JavaScript code (although the underlying I/O subsystem of the computer may be doing things in parallel).

    This certainly fails the philosophy behind node.js right??

    No. The philosophy of node.js is to execute your event handlers as quickly as possible once I/O events are ready for action.

    Note that your “sleep” function doesn’t really sleep, instead it pegs the CPU – since node is single-threaded all other actions will block on the CPU crunching code – the same would happen if your code was doing some actual CPU intensive actions. However, if your code was instead performing I/O operations (and it was designed properly), then node will schedule other actions around your I/O blocking code. Think of it this way – node.js prevents code from blocking on I/O, not from blocking on CPU usage. If your code is CPU intensive and you’re worried about it blocking other handlers then you must design it in a way to yield to the event loop to let other handlers run.

    Another observation in this scenario, was that the requests were not handled chronologically – they are executed randomly.

    Yes, this is possible. You must remember that node.js is essentially just a way to attach “event handlers” to I/O events of interest. These I/O events are triggered by actions in the underlying operating system (e.g. a socket connection has been established, a file read has completed, etc.) and node.js calls your handlers in response.

    Since the operating system does its own “internal bookkeeping” about when things actually happen and when it thinks they are available for “user space” processes, there may be a difference between when you expect them to happen and when the computer says they actually happen. Moreover, I don’t think that node (or perhaps even the OS) guarantee “fairness” when scheduling events.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to WP7 programming and I have been following this tutorial http://weblogs.asp.net/scottgu/archive/2010/03/18/building-a-windows-phone-7-twitter-application-using-silverlight.aspx
I'm new to NHibernate... I have been following this NHibernate Tutorial from Gabriel Schenker
I have been following this tutorial and everything was going nice and easy until
I have been following this tutorial series for OpenGL : GLUT : http://www.lighthouse3d.com/opengl/glut/ I
i have been following this tutorial for the pagerAdapter. http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/ My problem is that
I have been following this tutorial, http://ruby.railstutorial.org/chapters/modeling-users?version=3.2#top , which I find great, but it
I have been following this tutorial, http://ruby.railstutorial.org/chapters/modeling-users?version=3.2#top , and I tried this in the
I have been following this tutorial, online http://ruby.railstutorial.org/chapters/sign-in-sign-out?version=3.2#top and in part 8.2.3 there is
I have been following this tutorial. I'm having conceptual trouble at the point where
I have been learning RESTful webservices following this tutorial http://www.vogella.de/articles/REST/article.html . As I understand,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.