0

I want understand, how asynchronous functions work in nodejs internally. Suppose, I want read any file from filesystem:

fs = require('fs')
fs.readFile('/etc/hosts', 'utf8', function (err,data) {
  if (err) {
    return console.log(err);
  }
  console.log(data);
});

What is going on, when I call fs.readFile(...)? Or, in another words, how v8 interpreter interacts with operating system (filesystem), when I call fs.readFile(...)? How v8 knows, that file reading is done, and callback must be called?

igor_rb
  • 1,821
  • 1
  • 19
  • 34
  • 1
    There are two things it could do. Spawn a new thread and perform the read operation there or try and use non blocking io. http://www.kegel.com/dkftpbench/nonblocking.html – Tesseract Apr 05 '15 at 08:56
  • Thanks, I don't know about non blocking io concept before, and you comment was very helpful for me. I found similar question, after you comment: http://stackoverflow.com/q/14795145/2568343 . All asyncronous code based on libuv https://github.com/libuv/libuv – igor_rb Apr 05 '15 at 20:44

0 Answers0