0
0
Node.jsframework~10 mins

Removing 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 remove the listener from the event emitter.

Node.js
emitter.removeListener('data', [1]);
Drag options to blanks, or click blank then click option'
Aonce
BonData
Cemit
DaddListener
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the event name instead of the listener function.
Using addListener instead of the listener function.
Trying to remove a listener that was never added.
2fill in blank
medium

Complete the code to remove all listeners for the 'end' event.

Node.js
emitter.[1]('end');
Drag options to blanks, or click blank then click option'
AremoveListener
Boff
Conce
DremoveAllListeners
Attempts:
3 left
💡 Hint
Common Mistakes
Using removeListener which removes only one listener.
Using once which adds a one-time listener.
Using off which is an alias but not always available.
3fill in blank
hard

Fix the error in removing a listener by completing the code.

Node.js
emitter.off('message', [1]);
Drag options to blanks, or click blank then click option'
AhandleMessage
BhandleMessage()
C'handleMessage'
DmessageHandler
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the function with parentheses which executes it immediately.
Passing the function name as a string instead of a function reference.
Passing a different function name than the one added.
4fill in blank
hard

Fill both blanks to remove a listener added with once.

Node.js
const listener = () => console.log('done');
emitter.[1]('finish', listener);
emitter.[2]('finish', listener);
Drag options to blanks, or click blank then click option'
Aonce
BremoveListener
CremoveAllListeners
Don
Attempts:
3 left
💡 Hint
Common Mistakes
Using on instead of once to add the listener.
Using removeAllListeners which removes all listeners, not just one.
Not passing the same listener function to remove it.
5fill in blank
hard

Fill all three blanks to remove a named listener after adding it.

Node.js
function logData(data) {
  console.log(data);
}
emitter.[1]('data', logData);
emitter.[2]('data', logData);
const count = emitter.listenerCount([3]);
Drag options to blanks, or click blank then click option'
Aon
BremoveListener
C'data'
Donce
Attempts:
3 left
💡 Hint
Common Mistakes
Using once instead of on to add the listener.
Passing the function name as a string instead of the function reference when removing.
Not passing the event name string to listenerCount.