0
0
Node.jsframework~10 mins

Events vs callbacks decision in Node.js - Interactive Practice

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

Complete the code to create an event emitter instance.

Node.js
const EventEmitter = require('events');
const emitter = new [1]();
Drag options to blanks, or click blank then click option'
AEmitterEvent
BEmitter
CEvent
DEventEmitter
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong class name like 'Emitter' or 'Event'.
Forgetting to use parentheses to create an instance.
2fill in blank
medium

Complete the code to add a callback function for the 'data' event.

Node.js
emitter.on('data', [1] => {
  console.log('Data received:', data);
});
Drag options to blanks, or click blank then click option'
A()
Bfunction(data)
Cdata
Devent
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'function(data)' without arrow syntax here.
Using empty parentheses which means no parameters.
3fill in blank
hard

Fix the error in the callback usage by completing the code.

Node.js
function fetchData(callback) {
  setTimeout(() => {
    const result = 'Hello';
    callback([1]);
  }, 1000);
}
Drag options to blanks, or click blank then click option'
Aresult
Bcallback
CfetchData
DsetTimeout
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the callback function itself instead of data.
Passing unrelated function names.
4fill in blank
hard

Fill both blanks to emit an event with a message and listen for it.

Node.js
emitter.[1]('message', 'Hi there!');
emitter.[2]('message', msg => {
  console.log(msg);
});
Drag options to blanks, or click blank then click option'
Aemit
Bon
Csend
Dlisten
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' or 'listen' which are not Node.js EventEmitter methods.
Mixing up 'emit' and 'on' methods.
5fill in blank
hard

Fill all three blanks to decide between callback and event for async handling.

Node.js
function processData(data, [1]) {
  if (data.length > 5) {
    [2]('error', new Error('Too long'));
  } else {
    [3](null, data);
  }
}
Drag options to blanks, or click blank then click option'
Acallback
Bemitter.emit
Demitter.on
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'emitter.on' instead of 'emitter.emit' to send events.
Confusing callback with event emitter methods.