0

I make the following test with file_zero a big file.

var fs = require('fs');

fs.readFile('file_zero', 'utf8', function(){
    console.log("ICIIIII")
});

fs.readFile('file_zero', 'utf8', function(){
    console.log("ICIIIII2222222")
});

We observe that the both reads are made at the same time more or less. The ouputs are printed quite at the same time. So there is not one file read then the second one. And the second one can print before the first one.

So my question is how readFile is implemented (and in general all the asynchonous functions)? Javascript is one mono-thread. So how the reads can be made in parallel?

I guess that the runtime has a queue of jobs and that alterantes between the different jobs to make the behavior of a kind of parallel?

Maybe I am wrong so correct me :)

So if it is like that, it is not possible to implement such an asynchronous function with pure javascript (except creating a runtime environment).

Thank you

El pupi
  • 458
  • 1
  • 3
  • 13

0 Answers0