Bird
Raised Fist0
Node.jsframework~20 mins

Once listeners in Node.js - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Once Listener Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when an event listener is added with once()?
Consider the following Node.js code using an EventEmitter. What will be the output after emitting the event twice?
Node.js
const EventEmitter = require('events');
const emitter = new EventEmitter();

emitter.once('ping', () => {
  console.log('Ping received');
});

emitter.emit('ping');
emitter.emit('ping');
A
Ping received
Ping received
BPing received
CNo output
DError: Event listener called twice
Attempts:
2 left
💡 Hint
The once() method adds a listener that is removed after it is called once.
🔧 Debug
intermediate
2:00remaining
Why does this once listener not fire?
Look at this code snippet. Why does the listener never log anything?
Node.js
const EventEmitter = require('events');
const emitter = new EventEmitter();

function onData() {
  console.log('Data received');
}

emitter.once('data', onData);

// Later in code
emitter.removeListener('data', () => {
  console.log('Data received');
});
emitter.emit('data');
ABecause removeListener uses a different function reference, so the once listener is removed before emit
BBecause once listeners never fire if removeListener is called
CBecause the event name 'data' is misspelled
DBecause emit is called before once listener is added
Attempts:
2 left
💡 Hint
removeListener requires the exact same function reference to remove a listener.
📝 Syntax
advanced
2:00remaining
Which option correctly adds a once listener that logs the event argument?
You want to add a once listener to an EventEmitter that logs the data passed with the event. Which code is correct?
Aemitter.once('message', (data) => console.log(data));
Bemitter.once('message', function { console.log(data); });
Cemitter.once('message', () => { console.log(data); });
Demitter.once('message', (data) => { console.log(data) });
Attempts:
2 left
💡 Hint
Arrow functions can receive parameters directly.
state_output
advanced
2:00remaining
What is the value of count after emitting an event with a once listener?
Given this code, what is the value of count after emitting 'update' event three times?
Node.js
const EventEmitter = require('events');
const emitter = new EventEmitter();
let count = 0;

emitter.once('update', () => {
  count += 1;
});

emitter.emit('update');
emitter.emit('update');
emitter.emit('update');
A3
BError: count is not defined
C0
D1
Attempts:
2 left
💡 Hint
once listeners run only once no matter how many times the event is emitted.
🧠 Conceptual
expert
2:00remaining
Which statement about once listeners is true?
Select the correct statement about once listeners in Node.js EventEmitter.
AOnce listeners must be manually removed using removeListener after being called.
BA once listener can be called multiple times if the event is emitted rapidly.
CA once listener is automatically removed after it is called once, preventing memory leaks.
DOnce listeners do not receive event arguments.
Attempts:
2 left
💡 Hint
Think about how once listeners help manage event listeners lifecycle.

Practice

(1/5)
1. What is the main behavior of a once listener in Node.js event handling?
easy
A. It runs twice before removing itself automatically.
B. It runs only the first time the event is emitted and then removes itself.
C. It runs every time the event is emitted without removing itself.
D. It never runs automatically and must be triggered manually.

Solution

  1. Step 1: Understand the purpose of once listeners

    Once listeners are designed to handle an event only once, meaning they execute their callback a single time.
  2. Step 2: Recognize automatic removal behavior

    After running once, the listener removes itself automatically to prevent further executions.
  3. Final Answer:

    It runs only the first time the event is emitted and then removes itself. -> Option B
  4. Quick Check:

    Once listener = runs once then removes [OK]
Hint: Once listeners run once then auto-remove themselves [OK]
Common Mistakes:
  • Thinking once listeners run multiple times
  • Confusing once with on listeners
  • Assuming manual removal is needed
2. Which of the following is the correct syntax to add a once listener to an EventEmitter instance emitter for event 'start'?
easy
A. emitter.once('start', () => { console.log('Started'); });
B. emitter.on('start', () => { console.log('Started'); });
C. emitter.once('start', console.log('Started'));
D. emitter.once('start', function console.log('Started'));

Solution

  1. Step 1: Identify the correct method for once listeners

    The method to add a once listener is once, not on.
  2. Step 2: Check the callback syntax

    The callback must be a function, so () => { console.log('Started'); } is correct. Passing the result of console.log directly is wrong.
  3. Final Answer:

    emitter.once('start', () => { console.log('Started'); }); -> Option A
  4. Quick Check:

    Use emitter.once(event, callback) with a function [OK]
Hint: Use emitter.once with a function callback [OK]
Common Mistakes:
  • Using emitter.on instead of emitter.once
  • Passing console.log() call instead of function
  • Incorrect function syntax in listener
3. Consider this code snippet:
const EventEmitter = require('events');
const emitter = new EventEmitter();

emitter.once('ping', () => console.log('Ping received'));
emitter.emit('ping');
emitter.emit('ping');

What will be the output?
medium
A. No output
B. Ping received Ping received
C. Ping received
D. Error thrown

Solution

  1. Step 1: Understand emitter.once behavior

    The listener runs only once on the first emit('ping') call.
  2. Step 2: Analyze the two emit calls

    The first emit('ping') triggers the listener and prints 'Ping received'. The second call finds no listener because it was removed after first run.
  3. Final Answer:

    Ping received -> Option C
  4. Quick Check:

    once listener runs once, so output once [OK]
Hint: once listeners run only on first emit call [OK]
Common Mistakes:
  • Expecting output on second emit
  • Confusing once with on listeners
  • Assuming listener stays after first run
4. What is wrong with this code snippet?
const EventEmitter = require('events');
const emitter = new EventEmitter();

emitter.once('data', console.log('Data event'));
emitter.emit('data');
medium
A. The listener callback is called immediately, not on event emit.
B. The event name 'data' is invalid for once listeners.
C. The emitter.emit call is missing a callback function.
D. The once method does not exist on EventEmitter.

Solution

  1. Step 1: Check how the callback is passed

    The code passes console.log('Data event') which calls console.log immediately and passes its return (undefined) as listener.
  2. Step 2: Understand correct callback usage

    The callback should be a function, e.g., () => console.log('Data event'), to run only on event emit.
  3. Final Answer:

    The listener callback is called immediately, not on event emit. -> Option A
  4. Quick Check:

    Pass function, not function call, as listener [OK]
Hint: Pass function, not function call, as listener [OK]
Common Mistakes:
  • Calling function instead of passing it
  • Assuming event name invalid
  • Thinking once method is missing
5. You want to log a message only the first time a user connects to your server using an EventEmitter server. Which code snippet correctly achieves this and prevents multiple logs if the user reconnects?
hard
A. server.on('connect', () => console.log('User connected'));
B. server.on('connect', console.log('User connected'));
C. server.once('connect', console.log('User connected'));
D. server.once('connect', () => console.log('User connected'));

Solution

  1. Step 1: Identify the requirement for single execution

    Logging only once means the listener should run once and then remove itself.
  2. Step 2: Choose the correct method and callback syntax

    server.once('connect', () => console.log('User connected')) uses the once method with a function callback, ensuring single log on first connect.
  3. Final Answer:

    server.once('connect', () => console.log('User connected')); -> Option D
  4. Quick Check:

    Use once with function callback for single event handling [OK]
Hint: Use once with function callback to run once [OK]
Common Mistakes:
  • Using on instead of once for single execution
  • Passing function call instead of function
  • Expecting multiple logs with once listener