我将用例子解释我的问题。

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

当这行代码被执行时,readFile 将开始读取文件。在成功或失败时,此回调将被插入队列以使用适当的参数执行。

正在读取文件 asynchronously ,但是如果 NodeJS 是单线程、非阻塞的,那么谁在做呢?

最佳答案

用户代码在单个线程中执行。然而,在后台,nodejs 使用 libuv/libio 来处理任何使用线程的 io。

https://github.com/libuv/libuv

有趣的帖子:
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?

关于javascript - NodeJS : Who does the asynchronous work when a async function is called?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35348893/

10-12 07:38
查看更多