Javascript is single threaded. So every piece of code which I write is executed in a single thread of javascript runtime provided by Node. So when I execute following code:
var fs = require("fs");
fs.readFile('input.txt', function (err, data) {
if (err) {
return console.error(err);
}
console.log("Asynchronous read: " + data.toString());
});
// Synchronous read
var data = fs.readFileSync('input.txt');
Following is my understanding of workflow, please correct me if I am wrong:
fs.readFile is popped off the call stack by manager in Node immediately and moved to some other thread where it starts the reading stuff.
When reading is complete, manager in Node:
- puts the callback function of
fs.readFilein the queue stack - which event loop eventually puts in the call stack and executes
fs.readFileSync is not popped off the stack but is executed in the runtime thread itself like any other function.
So, who is this manager in node which executes Node API I/O functions in other threads and manages their call stacks.
So does Node use multi-cores to execute it's API functions in the background?
