3

I am new to node.js and little bit confused on understanding the event-loop. As far as i know from https://github.com/nodejs/node/blob/master/doc/topics/event-loop-timers-and-nexttick.md, the event-loop phases only process setTimeout, setInterval, setImmediate, process.nextTick, promises and some I/O callbacks.

My question is, if i have following code:

for (var i = 0; i < 100000000; i++) ;

in which phase the above code will get executed ?

10
  • Unless I'm missing something, I wouldn't expect a for-loop to be considered an "event" to be processed by an event loop. Commented Jan 17, 2017 at 13:51
  • I/O callbacks: executes almost all callbacks.... In node an HTTP server handling a request is I/O. Commented Jan 17, 2017 at 13:52
  • so, where will be the code get executed. Is it executed via callstack ? Commented Jan 17, 2017 at 13:53
  • The event loop is a queue. If I'm not mistaken, these phases are different priority queues. Commented Jan 17, 2017 at 13:56
  • If it won't be processed by event-loop, will you consider that the code itself doesn't block the event-loop ? Commented Jan 17, 2017 at 13:57

2 Answers 2

4

Regular JavaScript code, like the for loop in your example, is executed before the queues are cleared. The first thing node will do is run your code, and will only call callbacks, timeout results, I/O results, and so on after your code finishes.

As an example, you could try this code:

fs.open('filename', 'r', () => {
  console.log('File opened.');
});

for (var i = 0; i < 100000000; i++);

console.log('Loop complete.');

No matter how big or small your loop variable, 'Loop complete' will always appear before 'File opened'. This is because with only one thread, node can't run the callback you've supplied to the fs.open function until the loop code has finished.

Remember that there isn't a "main" thread that node keeps going back to. Most long-running node programs will run through the code in main.js pretty quickly, and subsequent code is all going to come from callbacks. The purpose of the initial execution is to define how and when those callbacks happen.

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

Comments

0

In the node event loop doc (https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick), the following code is given as an example:

const fs = require('fs');

function someAsyncOperation(callback) {
  // Assume this takes 95ms to complete
  fs.readFile('/path/to/file', callback);
}

const timeoutScheduled = Date.now();

setTimeout(() => {
  const delay = Date.now() - timeoutScheduled;

  console.log(`${delay}ms have passed since I was scheduled`);
}, 100);


// do someAsyncOperation which takes 95 ms to complete
someAsyncOperation(() => {
  const startCallback = Date.now();

  // 10ms loop
  while (Date.now() - startCallback < 10) {
    // do nothing
  }
});

The loop keeps scanning according to phases and after fs.readFile() finishes, the poll queue is is empty, so its callback will be added and immediately executed. The callback holds a blocking 10ms loop before the timer is executed. That is why the delay will display: 105ms have passed since I was scheduled instead of the 100ms you might expect.

Most of your code will live in callbacks so will be executed in the poll phase. If not, like in your example, it will be executed before entering any phases as it will block the event loop.

The caveat are callbacks scheduled by setImmediate that will enter the check phase before resuming the poll phase in the next loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.