0
0
Node.jsframework~10 mins

Creating worker threads 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 import the Worker class from the worker_threads module.

Node.js
const { [1] } = require('worker_threads');
Drag options to blanks, or click blank then click option'
AThread
BWorker
CWorkerThread
DThreadWorker
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names like Thread or WorkerThread.
Trying to import the whole module without destructuring.
2fill in blank
medium

Complete the code to create a new worker thread running the script 'worker.js'.

Node.js
const worker = new Worker('[1]');
Drag options to blanks, or click blank then click option'
A'./worker'
B'worker.js'
C'./worker.js'
D'worker'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the './' prefix causing module not found errors.
Leaving out the '.js' extension.
3fill in blank
hard

Fix the error in the code to listen for messages from the worker.

Node.js
worker.on('[1]', (msg) => {
  console.log('Message from worker:', msg);
});
Drag options to blanks, or click blank then click option'
Amessage
Bmsg
Cdata
Dsend
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect event names like 'msg' or 'data'.
Trying to listen for 'send' event which does not exist.
4fill in blank
hard

Fill both blanks to send a message 'Hello' to the worker and listen for its response.

Node.js
worker.[1]('Hello');
worker.on('[2]', (response) => {
  console.log('Received:', response);
});
Drag options to blanks, or click blank then click option'
ApostMessage
Bmessage
Csend
DonMessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' instead of 'postMessage' to send data.
Listening for 'onMessage' event which does not exist.
5fill in blank
hard

Fill all three blanks to create a worker, send it data, and handle errors.

Node.js
const worker = new [1]('./task.js');
worker.[2]({ task: 'compute' });
worker.on('[3]', (err) => {
  console.error('Worker error:', err);
});
Drag options to blanks, or click blank then click option'
AWorker
BpostMessage
Cerror
DThread
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Thread' instead of 'Worker' to create the worker.
Using 'send' instead of 'postMessage' to send data.
Listening for 'err' event instead of 'error'.