Let’s look at a code snippet first:

setImmediate(()=> console.log('setImmediate'));

fs.readFile('/etc/passwd',(err, data)=>{
  console.log('reading file');
}); 
console.log('start');
process.nextTick(()=> console.log('nextTick'));
setTimeout(()=>console.log('setTimeout 1'), 0);
setTimeout(()=>console.log('setTimeout 2'), 3);
let counter = 0;
const timeout = setInterval(() => {
    console.log('setInterval');
    if (counter >= 3) {
        console.log('exiting setInterval');
        clearInterval(timeout);
    }
    counter++;
}, 0);
new Promise((resolve, reject)=> {
  console.log('start promise 1');
  resolve('Promise 1');
}).then(data=> {
  console.log(data);
})
console.log('end');

What would be the output of this program?

In order to answer the question, we’ll need to understand 2 things:

  • The different phases in the event loop.
  • The tasks each phase will handle.

#nodejs #event-loop #software-engineering #javascript

The 6 phases of the Node.js event loop explained
7.20 GEEK