0

I am seeing some demos of node js

setInterval(function(){
    console.log('world');
}, 2000);

console.log('hello');

I have installed node js for Windows, this prints hello and then world when I run it from command prompt as per instruction at a regular interval but within that interval I am not able to do anything. Am I missing something in Windows 7

5
  • What do you mean by "not able to do anything"? Commented Jan 12, 2014 at 20:16
  • @herby as per tuto by Ryan Dahl, the process is on ideal state rather than sleep so we can do things within that interval like in this case on cmd prompt within those timeinterval could type in some random text. Or am I not getting his point?? Commented Jan 12, 2014 at 20:20
  • If you're running it like c:\node server.js, you're running the code, and the interval is async, but you can't type on the command line ? Commented Jan 12, 2014 at 20:33
  • @adeneo yes I am running like C:\node D:\server.js Commented Jan 12, 2014 at 20:39
  • Then the DOS window will be blocked, and that has nothing to do with the asynchronous nature of node.js, it's got something to do with how Windows executes the program. Commented Jan 12, 2014 at 20:52

3 Answers 3

2

I think you're misunderstanding what setInterval does. It's a blocking function that waits for the designated timeout (2000ms in your case) and then executes the contained function after the time expires.

Because Node.js is non-blocking (meaning it executes the entire code block in a single asynchronous stream), it will print hello before it prints the first world.

Explained:

setInterval(function(){
    console.log('world');
    // 2. This prints out after 2 seconds, and prints again every 2 seconds
}, 2000);

console.log('hello');
// 1. This prints out

In short, what you're seeing is the expected result.

Sign up to request clarification or add additional context in comments.

3 Comments

the process is on ideal state rather than sleep so we can do things within that interval like in this case on cmd prompt within those timeinterval could type in some random text. Or am I not getting his point??
I have no idea what that sentence means, only that because setInterval is blocking (it halts the thread) you can't do anything. You could set a bunch of other things to run, like make an API call, or something, which allows the rest of the script to continue on while the API call completes asynchronously.
@adeneo rather, it halts the function not the thread sorry. If something else were to be written after console.log('world');, it would not occur until after; whereas console.log('hello'); happens outside the setInterval.
0

You misunderstood the words of the tutorial. When tutorial says "the process is on idle state rather than sleep so we can do things within that interval" it does not mean that the process runs in the background; it just means that the process itself can do a lot of things in those two seconds, unlike "sleep" command which will block and wait.

Running in the background is entirely different thing and is independent of the way how process itself works. It depends on the shell in which you are. In UNIX, you can run any process (no matter if it is non-blocking node or blocking wait or whatever else) by typing process arguments & (the & is important). But Windows command prompt does not have this (the nearest thing you can get is start node script.js, which runs it in new window and returns the prompt in previous window immediately.

IOW, it works exactly as expected.

3 Comments

do we need a start, I think node script.js is enough
@brykneval - In very simple terms, the answer is saying that what you're doing is async, and within node it does not block the thread, and you can do other stuff in your .js file, but that's not the same as doing more stuff in the window with the command line, as that has nothing to do with Node and is controlled by the OS.
Yes, it is enough, of course. Just you expected it to run in the background. You can use start to more or less get it. But it of course does not change the meaning of the code, just the mode by which you run it. Didn't you understand? I mentioned start in a paragraph about running in the background, it has nothing to do with actual node.js or the code of the script. Should I delete it completely if it confuses you?
0

I think your problem is understanding what is meant by "so we can do things". setInterval sets up a timer so that the specified function will be called every time the timer is up.

In the example given, the timer is every 2 seconds. This means that you can do something else in node.js while the timer is counting down. However in node.js there is only one thread so only one thing can execute at a time. The timer just gives the ability to say, when this time is up do something when nothing else is running.

So, for example, you could process network requests while the timer is counting down, you could read the contents of a file, you could parse some xml, etc.

I wrote code that uses a timer to check if a set of data was dirty. If it was then it would transmit the changes to all other clients. I had a listener on socket that would receive telemetry from a connected drone. If I cared about the telemetry then I would update the data. So I could update the data multiple times in a minute, while only check if I need to send an update every minute. So while the setInterval had set a repeating timer of every minute, when the function for the timer wasn't being executed, node.js could process any events from the listeners on the socket.

Note however, if your doing something during the time when the timer is up, then it node.js will have to wait until you finish before it can let the timer function execute.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.