I did some reading and also dug through the Node.js source code. I now understand how the close()
and on()
functions work.
First, regarding the close()
function, I had misunderstood that it adds a callback on the queue of the close callbacks phase. That's actually not true. In reality, the close()
function uses a promise to close the file. In the callback that's executed when the promise resolves, the close event is emitted using process.nextTick()
.
As for the on()
function, it simply adds a function to the list of handlers associated with a particular event.