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

  • Home
  • SEARCH
  • 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 8670275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:40:27+00:00 2026-06-12T18:40:27+00:00

Knowing that while Node.js is working asynchronously, writing something like this: function sleep() {

  • 0

Knowing that while Node.js is working asynchronously, writing something like this:

function sleep() {
    var stop = new Date().getTime();
    while(new Date().getTime < stop + 15000) {
        ;
    }
}

sleep();
console.log("done");

…would call the sleep(), block the server for the duration of the while loop (15secs) and just THEN print “done” to the console. As far as I understand, this is because Node.js is giving JavaScript only access to the main thread, and therefore this kidn of thing would halt further execution.

So I understand the solution to this is to use callbacks:

function sleep(callback) {
    var stop = new Date().getTime();
    while(new Date().getTime() < stop + 15000) {
        ;
    }
    callback();
}

sleep(function() {
    console.log("done sleeping");
});

console.log("DONE");

So I thought this would print ‘DONE’ and after 15 secs. ‘done sleeping’, since the sleep() function gets called and is handed a pointer to a callback function. While this function is working (the while loop), the last line would be executed (print ‘done’). After 15 seconds, when the sleep() function finishes, it calls the given callback function, which then prints ‘done sleeping’.

Apparently I understood something wrong here, because both of the above ways block. Can anybody clarify please?

Thanks in advance,
Slagjoeyoco

  • 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-12T18:40:28+00:00Added an answer on June 12, 2026 at 6:40 pm

    Javascript and node.js are single threaded, which means a simple while blocks; no requests/events can be processed until the while block is done. Callbacks don’t magically solve this problem, they just help pass custom code to a function. Instead, iterate using process.nextTick, which will give you esentially the same results but leaves space for requests and events to be processed as well, ie, it doesn’t block:

    function doSleep(callback) {
        var stop = new Date().getTime();
    
        process.nextTick(function() {
            if(new Date().getTime() < stop + 15000) {
                //Done, run callback
                if(typeof callback == "function") {
                    callback();
                }
            } else {
                //Not done, keep looping
                process.nextTick(arguments.callee);
            }
        });
    }
    
    doSleep(function() {
        console.log("done sleeping");
        console.log("DONE");
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I set cookies like this: $_SESSION[md5('ponies'.$salt)] = $value; so that only knowing the
I know that it might sound silly but, while working on project at some
This has bothered me for quite a while, but now it is necessity that
How would you go about knowing that ajax requests are related? Normally with HTTP-HTML
I wrote a program that worked as a server. Knowing that accept was blocking
I've been using Mootools for the last couple of months without knowing that MS
I need to be able to access a property via reflection, and, knowing that
In Android, assuming that I have files in /data/data/package.name/, without knowing the names or
Without knowing i typed 'far' instead of 'var' by my mistake.I noticed the keyword
I am currently working on a site that includes javascript code that we get

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.