Node JS - How to make custom event so that it is processed in the close phase of the event loop?

141 Views Asked by At

Taking a quote from the NodeJS event loop documentation.

close callbacks

If a socket or handle is closed abruptly (e.g. socket.destroy()), the 'close' event will be emitted in this phase. Otherwise it will be emitted via process.nextTick().

First of what does the above lines mean: does that mean the event will be generated at this phase or will the callback for the event be executed at this phase ?

If the latter is true then is the socket.destroy() event, the only event that is handled in the close phase of the event loop ? What about finish event ?

Also are close events somehow special that they will be handled in a separate phase of the event loop ?

What if in an EventEmitter I write emmiter.emit('exe_finish'), but I want that event to be handled that at the close callbacks phase of the event loop, how do I do that ?

It seems to me that no matter what events I emit they will be handled at the poll/IO Callback phase of the event loop.

How do I explicitly state that my custom event is supposed to be handled in the close callbacks phase of the event loop ?

Is it even possible to do this from within NodeJS? Or do I have to write my own node module in C++ to do that ?

1

There are 1 best solutions below

0
On

See this guide, it states

If a socket or handle is closed abruptly (e.g. socket.destroy()), the 'close' event will be emitted in this phase. Otherwise it will be emitted via process.nextTick().

The handling of the close callbacks is not bound to any eventName but rather to the underlying resource (e.g. a socket). So I think there is no way for you to declare custom events to be handled in the close callback phase.