Both are completely different. The browser event loop is independent of I / O operations . But the Node js event loop is dependent on I / O. Here, the main purpose of the Node js event loop is to separate the main process and try to perform I / O and other timer APIs asynchronously.
And another difference is that we do not have the setImmediate () function in the browser. The difference between setTimeout () and setImmediate () is that the setTimeout () callback function will execute after the specified minimum threshold value in milliseconds. But in setImmediate () after performing any I / O operation, if a specific code is specified inside setImmediate (), it will be executed first.
Because usually
setTimeout(() => { //some process }, 0);
and
setImmediate(() => { //some process });
are the same, and we cannot predict what will be done first. But in the future, Node js under the nodejs event loop mechanism, if both of them are present on any I / O callback , setImmediate () will be executed first. So,
let fs = require('fs'); fs.readFile('/file/path', () => { setTimeout(() => { console.log('1'); }, 0); setImmediate(() => { console.log('2'); }); });
The output for the code above will be,
2 1
Prince devadoss
source share