0
0
Node.jsframework~10 mins

Why worker threads matter in Node.js - Test Your Understanding

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

Complete the code to create a new worker thread.

Node.js
const { Worker } = require('worker_threads');
const worker = new [1]('./worker.js');
Drag options to blanks, or click blank then click option'
AWorker
BChild
CProcess
DThread
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Thread' or 'Process' instead of 'Worker'.
Confusing worker threads with child processes.
2fill in blank
medium

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'
Adata
Bmessage
Csend
Dreceive
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' or 'receive' which are not event names.
Using 'data' which is not the correct event for worker messages.
3fill in blank
hard

Fix the error in the code to properly terminate a worker thread.

Node.js
worker.[1]();
Drag options to blanks, or click blank then click option'
Astop
Bclose
Cterminate
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'stop' or 'end' which do not exist on worker objects.
Using 'close' which is not a method for worker termination.
4fill in blank
hard

Fill both blanks to send a message from the main thread to the worker.

Node.js
worker.[1]([2]);
Drag options to blanks, or click blank then click option'
ApostMessage
Bsend
C'Hello, worker!'
Dmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'send' instead of 'postMessage'.
Not passing a message or passing an event name instead.
5fill in blank
hard

Fill all three blanks to create a worker that runs a function from a file and handle its exit event.

Node.js
const worker = new Worker('./task.js', { [1]: true });
worker.on('[2]', (code) => {
  console.log('Worker exited with code', [3]);
});
Drag options to blanks, or click blank then click option'
Aeval
Bexit
Ccode
DrunInNewContext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'runInNewContext' instead of 'eval'.
Listening to 'close' instead of 'exit' event.
Logging a wrong variable instead of the exit code.