nodejs runs on single thread, and when I read a file asynchronously then nodejs will run a thread in background to read that file, I believe the idea is to use as many CPU cores as possible.
Consider following snippet,
fs.readdirSync('my/path', (err, files) => {
if (err) throw err;
for(let i=0; i<files.length; i++){
fs.readFile(files[i], (err, data) => {
if (err) throw err;
console.log(data);
});
}
});
Now, assuming the no. of files in my/path
is 10. Now since nodejs uses one thread delicately for the eventloop, it is left with 7 CPU cores. How will nodejs handle this ? Is there any internal queue within nodejs or is it purely operating system implementation ?