Chapter 9 EventEmitter and Listeners doing work asynchronously
Hi,
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.
https://nodejs.org/docs/latest-v12.x/api/events.html#events_asynchronous_vs_synchronous
From the docs on the process.nextTick() method, it runs after the current JavaScript operation completes and before the event loop is processed.
https://nodejs.org/dist/latest-v12.x/docs/api/process.html#process_process_nexttick_callback_args
This way of doing asynchronous call can possibly delay or even block the work to be done in the event loop.
- Is this understanding correct? If yes, how does one avoid using
process.nextTick()in implementations? Is
setImmediate()method to be used to do such work on the event loop?
https://nodejs.org/api/timers.html#timers_setimmediate_callback_argsIs it possible to use asynchronous functions directly as listeners?
const asyncListener = async () => {
aLongRuningTask()
}
anEmitter.on('anEvent', asyncListener)
- Also, is there an easier introduction to understand the event loop than the one provided in the NodeJS docs?
https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/
Thanks in advance!
Best,
Zorawar
Comments
-
hi @zorawar
terms:
tick: a single iteration around the event loop
task queue: the tasks scheduled to be executed in the event loop- 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.
- 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
- 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.
- 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.
1 -
Thanks @davidmarkclements ! i appreciate the information.
0
Categories
- All Categories
- 175 LFX Mentorship
- 175 LFX Mentorship: Linux Kernel
- 745 Linux Foundation IT Professional Programs
- 372 Cloud Engineer IT Professional Program
- 168 Advanced Cloud Engineer IT Professional Program
- 73 DevOps IT Professional Program - Discontinued
- 3 DevOps & GitOps IT Professional Program
- 98 Cloud Native Developer IT Professional Program
- 7.6K Training Courses & Learning Paths
- AI & ML Training
- Blockchain & Decentralized Identity Training
- Cloud & Containers Training
- Cybersecurity Training
- DevOps & Site-Reliability Training
- Linux Kernel Development Training
- Networking Training
- Open Source Best Practice Training
- System Administration Training
- System Engineering Training
- Web & Application Development Training
- 2 LFD103-JP クラス フォーラム
- 4 LFD210-CN Class Forum
- 764 LFD259 Class Forum
- 681 LFS101 Class Forum
- 2 LFS158-JP クラス フォーラム
- 162 LFS207 Class Forum
- 3 LFS207-DE-Klassenforum
- 4 LFS207-JP クラス フォーラム
- 61 LFS241 Class Forum
- 52 LFS242 Class Forum
- 42 LFS243 Class Forum
- 19 LFS244 Class Forum
- 4 LFS250-JP クラス フォーラム
- 166 LFS253 Class Forum
- 19 LFS256 Class Forum
- 1.4K LFS258 Class Forum
- 165 LFS261 Class Forum
- 26 LFS267 Class Forum
- 792 Hardware
- 202 Drivers
- 68 I/O Devices
- 37 Monitors
- 95 Multimedia
- 173 Networking
- 91 Printers & Scanners
- 87 Storage
- 768 Linux Distributions
- 81 Debian
- 67 Fedora
- 22 Linux Mint
- 13 Mageia
- 24 openSUSE
- 150 Red Hat Enterprise
- 31 Slackware
- 13 SUSE Enterprise
- 356 Ubuntu
- 465 Linux System Administration
- 31 Cloud Computing
- 73 Command Line/Scripting
- Github systems admin projects
- 98 Linux Security
- 78 Network Management
- 101 System Management
- 46 Web Management
- 106 Mobile Computing
- 18 Android
- 73 Development
- 1.2K New to Linux
- 1K Getting Started with Linux
- 392 Off Topic
- 121 Introductions
- 181 Small Talk
- 29 Study Material
- 945 Programming and Development
- 310 Kernel Development
- 617 Software Development
- 977 Software
- 369 Applications
- 182 Command Line
- 5 Compiling/Installing
- 68 Games
- 317 Installation
- Archived
- 2 LFD140 Class Forum
Upcoming Training
-
August 20, 2018
Kubernetes Administration (LFS458)
-
August 20, 2018
Linux System Administration (LFS301)
-
August 27, 2018
Open Source Virtualization (LFS462)
-
August 27, 2018
Linux Kernel Debugging and Security (LFD440)
