Complete the code to create a new worker thread.
const { Worker } = require('worker_threads');
const worker = new [1]('./worker.js');The Worker class is used to create new worker threads in Node.js.
Complete the code to listen for messages from a worker thread.
worker.on('[1]', (message) => { console.log('Received:', message); });
The 'message' event is emitted when a worker thread sends data back.
Fix the error in the code to properly terminate a worker thread.
worker.[1]();The terminate() method stops the worker thread immediately.
Fill both blanks to send a message from the main thread to the worker.
worker.[1]([2]);
Use postMessage to send data to the worker thread. The message can be any value, here a string.
Fill all three blanks to create a worker that runs a function from a file and handle its exit event.
const worker = new Worker('./task.js', { [1]: true }); worker.on('[2]', (code) => { console.log('Worker exited with code', [3]); });
Setting eval: true allows the worker to run code passed as a string. The exit event fires when the worker stops, providing the exit code.