Welcome to the Linux Foundation Forum!

Chapter 9 EventEmitter and Listeners doing work asynchronously


Chapter 9 of the course covers EventEmitter and Listeners that can be added for events.
I am trying to understand how to perform some work that takes a long time and which is initiated from a listener. The listener should be made asynchronous in that case.
The NodeJS docs state that for EventEmitter the emit() method runs synchronously and all the listeners that have been registered are also executed synchronously in the order that they were registered. Now for listeners that do some work that takes a long time it is possible to have the listeners execute that work asynchronously using setImmediate() and process.nextTick() methods.

From the docs on the process.nextTick() method, it runs after the current JavaScript operation completes and before the event loop is processed.
This way of doing asynchronous call can possibly delay or even block the work to be done in the event loop.

  1. Is this understanding correct? If yes, how does one avoid using process.nextTick() in implementations?
  2. Is setImmediate() method to be used to do such work on the event loop?

  3. Is it possible to use asynchronous functions directly as listeners?

const asyncListener = async () => {
anEmitter.on('anEvent', asyncListener)
  1. Also, is there an easier introduction to understand the event loop than the one provided in the NodeJS docs?

Thanks in advance!



  • davidmarkclements
    davidmarkclements Posts: 270
    edited November 2020

    hi @zorawar


    tick: a single iteration around the event loop
    task queue: the tasks scheduled to be executed in the event loop

    1. sort of - the function passed to process.nextTick is called after all the other tasks in the current tick of the event loop are processed (but before the microtask queue, e.g. promise resolution). In some cases it's unavoidable, which is why it exists. In terms of blocking, no matter what happens, you can always block the event loop. Blocking in a process.nextTick will block the end of the current tick, blocking in a setImmediate will block the beginning of the next tick.
    2. As the docs say, it depends on use case. But you're talking about a long running task and firing an event when that task finishes. So neither nextTick or setImmediate will cater to that. If the long running task is asynchronous you fire an emit when it finishes, either in a callback or after promise resolution. If it's synchronous then you simply call it fire an emit in a process.nextTick on the next line to ensures that a consumer of the emitter can attach listeners prior to it firing
    3. Using async functions with event emitters it's not recommended. Bear in mind that an async function returns a promise, which can reject. The event emitter internals call the listener function, and there is no handling of promises at all. So if aLongRunningTask throws you will have an unhandled rejection which, depending on your flags and node version will either cause the process to crash or it will put your process into an unknown state. Additionally did you mean to await aLongRunningTask(), because if not there is no reason to do this in the first place. If you did want to use an async listener with an event emitter, wrap the entire function body in a try catch and then call anEmitter.emit('error', err) with any caught errors. That's the only safe way to do it.
    4. Not that I know of, this stuff is particularly difficult to understand and usually I spend 4 hours in front of a whiteboard with people going over about 20 exercises before the penny drops.
  • zorawar

    Thanks @davidmarkclements ! i appreciate the information.


Upcoming Training