I know fs.writeFile()
is asynchronous and will be delayed to be executed.
For example:
fs.writeFile()
console.log("non-blocking") // last statement of an event handler function
will print non-blocking
immediately and then that thread will spend time to execute the operations to write files.
However, if the NodeJS App is single-threaded. When the thread starts to execute the operations to write files after executing console.log("non-blocking")
. The thread seems still being blocked, though the blocking status is delayed. The blocking should happen before console.log("non-blocking")
if fs.writeFile
is replaced by fs.writeFileSync
. When I use fs.writeFile
here, the blocking still seems to exist when the last statement of that event handler is finished and the file writing operation is started.
Is it true that the thread is blocked when the file-writing operations are started? Or the blocking will happen in the main thread anyway. If so, is there a way to avoid it?
I don't think the question is the same as How the single threaded non blocking IO model works in Node.js, because this question is mainly about how NodeJS interacts with Operation System to do file operations.