I am trying to understand how concurrency works in a single-threaded environment like nodejs.
Let's say I have this code:
var fs = require('fs');
fs.readFile('file1', function one(err, data) {
// some code...
});
fs.readFile('/file2', function two(err, data) {
// some code...
});
Now each fs.readFile
call is async. So, they are running concurrently. But if all this is happening in a single thread, then how is the concurrency achieved? Are function one
and function two
running on the same or different thread?
Basically, how does node.js handle concurrency?