3

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?

Majeed Siddiqui
  • 548
  • 2
  • 9

2 Answers2

4

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?

Community
  • 1
  • 1
Vadim
  • 17,897
  • 4
  • 38
  • 62
0

Node.js is an event-driven server. It does not mean though that it uses multi-threading.

Being event-driven, Node.js can handle multiple requests at the same time. Basically, when it receives a request, Node.js launches its execution. Whilst being executed, Node.js receives another request, it will launch its execution. And so on. When the first request has finished being executed and the result is available, it triggers an event notifying Node.js that this request has finished, then Node.js sends back the request result.

More information can be found on the Node.js website, and even more information here.

jeerbl
  • 7,537
  • 5
  • 25
  • 39