LFW211 JSNAD CH-9
I have a question regarding the following. The above page says that
`import { once, EventEmitter } from 'events'
const uneventful = new EventEmitter()
await once(uneventful, 'ping')
console.log('pinged!')`
Execution will pause on the line starting await once, until the registered event fires. If it never fires, execution will never proceed past that point.
Am I supposed to see the process keep waiting on the await statement? (What I am experiencing is that I don't see the 'pinged' message but process exists may be because nothing exists to keep the Event loop active)
Best Answer
-
In Chapter 14. Process & Operating System you will probably find the answer to your questions.
https://trainingportal.linuxfoundation.org/learn/course/nodejs-application-development-lfw211/process-operating-system/process-operating-system?page=4Some API's have active handles. An active handle is a reference that keeps the process open. For instance, net.createServer creates a server with an active handle which will stop the process from exiting by itself so that it can wait for incoming requests. Timeouts and intervals also have active handles that keep the process from exiting
So, in the original question the process terminates immediately without printing 'pinged!' because 'ping' event is not emitted and there are no active handles (i.e. roughly speaking activities that could potentially emit given event)
The same applies to Question 1. The process remain live as long as there is active timer.
As for Question 2. It's a bit complicated. As I can see the resource is fetched successfully and 'ping' is emitted indeed. However, the subscription for 'ping' is made after the event is emitted.
If you rewrite the code following way, you will see 'pinged!' in output
import { once, EventEmitter } from 'events' const uneventful = new EventEmitter() setImmediate(async () => { await once(uneventful, 'ping') console.log('pinged!') }) try { const response = await fetch('https://jsonplaceholder.typicode.com/todos/1') const data = await response.json() console.log('before emit') uneventful.emit('ping') console.log('after emit') } catch (error) { console.error(error) }2
Answers
-
Thanks @abraham.m.joseph for your questions.
I actually have no answer, but while investigating your questions, I have also faced some understanding
regarding the 'once' event.Question 1. Why the process is alive for 5 seconds. As there should not be any relation between the
setTimeout and await once(uneventful, 'ping')
import { once, EventEmitter } from 'events' const uneventful = new EventEmitter() setTimeout(() => { }, 5000) await once(uneventful, 'ping')Question 2. Let's forget about the setTimeout, Now if I make a fake API call and fire an emit event after the response is successful, Why the "pinged!" is not printed in console.
import { once, EventEmitter } from 'events' const uneventful = new EventEmitter() try { const response = await fetch('https://jsonplaceholder.typicode.com/todos/1') const data = await response.json() uneventful.emit('ping') } catch (error) { } await once(uneventful, 'ping') console.log('pinged!')1 -
Thanks a lot, @dmsheiko. got it.
0 -
Thanks a lot raselkarim7 for your thought provoking questions and @dmsheiko for the answers. I understand better now.
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
- 105 Mobile Computing
- 18 Android
- 72 Development
- 1.2K New to Linux
- 1K Getting Started with Linux
- 392 Off Topic
- 121 Introductions
- 181 Small Talk
- 29 Study Material
- 944 Programming and Development
- 310 Kernel Development
- 616 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)
