0
0
Node.jsframework~10 mins

Receiving results from workers 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 listen for messages from a worker thread.

Node.js
worker.on('[1]', (message) => {
  console.log('Received:', message);
});
Drag options to blanks, or click blank then click option'
Asend
Bdata
Cresult
Dmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'data' or 'result' instead of 'message' causes no event to fire.
2fill in blank
medium

Complete the code to send a message from the main thread to the worker.

Node.js
worker.[1]({ task: 'processData' });
Drag options to blanks, or click blank then click option'
ApostMessage
Bemit
CsendMessage
Dmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'emit' or 'sendMessage' will cause errors as they are not valid methods.
3fill in blank
hard

Fix the error in the code to correctly receive data from the worker.

Node.js
worker.on('message', ([1]) => {
  console.log('Data:', data);
});
Drag options to blanks, or click blank then click option'
Amsg
Bmessage
Cdata
Dresult
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than the variable inside causes a ReferenceError.
4fill in blank
hard

Fill both blanks to correctly handle errors and exit events from a worker.

Node.js
worker.on('[1]', (error) => {
  console.error('Worker error:', error);
});
worker.on('[2]', (code) => {
  console.log('Worker exited with code', code);
});
Drag options to blanks, or click blank then click option'
Aerror
Bexit
Cclose
Ddisconnect
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'close' or 'disconnect' instead of 'exit' will not detect worker termination correctly.
5fill in blank
hard

Fill all three blanks to create a map of worker IDs to their last received messages.

Node.js
const results = {};
workers.forEach(worker => {
  worker.on('[1]', (message) => {
    results[worker.[2]] = message;
  });
});
console.log(results[[3]]);
Drag options to blanks, or click blank then click option'
Amessage
BthreadId
Cworker.threadId
DworkerId
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'workerId' which does not exist, or 'worker.threadId' inside bracket notation incorrectly.