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:
#nodejs #event-loop #software-engineering #javascript