Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS: Who does the asynchronous work when a async function is called?

I will explain my question with example.

fs.readFile('somefilename', function(err, data) {
    ...doSomething...
});

When this line of code is executed readFile will start reading the file. On success or failure this callback will be pushed into queue for execution with appropriate arguments.

Reading of file is happening asynchronously, but who's doing it if NodeJS is single threaded, non-blocking?

like image 232
Majeed Siddiqui Avatar asked Dec 08 '25 07:12

Majeed Siddiqui


1 Answers

User code is executed in a single thread. However, behind the scenes nodejs uses libuv/libio to handle any io which does use threads.

https://github.com/libuv/libuv

Interesting posts:
How the single threaded non blocking IO model works in Node.js
Does node.js use threads/thread pool internally?
Why is Node.js single threaded?

like image 114
Vadim Avatar answered Dec 09 '25 23:12

Vadim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!