0
0
Node.jsframework~10 mins

Custom event emitter classes in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the EventEmitter class from the 'events' module.

Node.js
const [1] = require('events');
Drag options to blanks, or click blank then click option'
AEventEmitter
BEmitter
CEvent
DEmitterClass
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names like 'Emitter' or 'Event'.
Forgetting to require the 'events' module.
2fill in blank
medium

Complete the code to create a new class named MyEmitter that extends EventEmitter.

Node.js
class MyEmitter extends [1] {
  constructor() {
    super();
  }
}
Drag options to blanks, or click blank then click option'
AEmitter
BEventEmitter
CEvent
DEmitterClass
Attempts:
3 left
💡 Hint
Common Mistakes
Extending a wrong or undefined class.
Forgetting to call super() in the constructor.
3fill in blank
hard

Fix the error in the code to emit a 'start' event from the emitter instance.

Node.js
const emitter = new MyEmitter();
emitter.[1]('start');
Drag options to blanks, or click blank then click option'
Aemit
Bfire
CemitEvent
Dtrigger
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like 'trigger' or 'fire'.
Using 'emitEvent' which is not a method.
4fill in blank
hard

Fill both blanks to add a listener for the 'data' event that logs the received message.

Node.js
emitter.[1]('data', ([2]) => {
  console.log([2]);
});
Drag options to blanks, or click blank then click option'
Aon
Bmessage
Cdata
Dlisten
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'addListener' instead of 'on' (valid but not in options).
Using the event name as the parameter name.
5fill in blank
hard

Fill all three blanks to create an instance, add a listener for 'end', and emit the 'end' event.

Node.js
const [1] = new MyEmitter();
[1].on('end', () => {
  console.log('Finished');
});
[1].[2]('end');
Drag options to blanks, or click blank then click option'
Aemitter
Bemit
Ctrigger
Dfire
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method names like 'trigger' or 'fire'.
Using different variable names inconsistently.