0
0
Node.jsframework~10 mins

Once listeners 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 add a listener that runs only once.

Node.js
emitter.[1]('start', () => { console.log('Started'); });
Drag options to blanks, or click blank then click option'
AaddListener
Bon
Conce
Demit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'on' instead of 'once' causes the listener to run multiple times.
Using 'emit' instead of a listener method.
2fill in blank
medium

Complete the code to emit the 'ready' event once.

Node.js
emitter.[1]('ready');
Drag options to blanks, or click blank then click option'
Aemit
BaddListener
Conce
Don
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'once' to emit events instead of listen.
Using 'on' which only adds listeners.
3fill in blank
hard

Fix the error in adding a one-time listener for 'data' event.

Node.js
emitter.[1]('data', () => { console.log('Data received'); });
Drag options to blanks, or click blank then click option'
Aon
BremoveListener
Cemit
Donce
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'on' causes the listener to run multiple times.
Using 'emit' instead of adding a listener.
4fill in blank
hard

Fill both blanks to add a one-time listener and then emit the event.

Node.js
emitter.[1]('finish', () => { console.log('Finished'); });
emitter.[2]('finish');
Drag options to blanks, or click blank then click option'
Aonce
Bemit
Con
DremoveListener
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'on' instead of 'once' for the listener.
Using 'on' instead of 'emit' to trigger the event.
5fill in blank
hard

Fill all three blanks to add a one-time listener, emit the event, and verify it runs only once.

Node.js
let count = 0;
emitter.[1]('ping', () => { count++; });
emitter.[2]('ping');
emitter.[3]('ping');
console.log(count);
Drag options to blanks, or click blank then click option'
Aonce
Bemit
Con
DremoveListener
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'on' instead of 'once' causes count to increase twice.
Using 'once' but not emitting the event.