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 sends data back to the main thread.
Complete the code to send a message from the main thread to the worker.
worker.[1]({ task: 'processData' });
The postMessage method sends data to the worker thread.
Fix the error in the code to correctly receive data from the worker.
worker.on('message', ([1]) => { console.log('Data:', data); });
The parameter name must match the variable used inside the function to avoid reference errors.
Fill both blanks to correctly handle errors and exit events from a worker.
worker.on('[1]', (error) => { console.error('Worker error:', error); }); worker.on('[2]', (code) => { console.log('Worker exited with code', code); });
The 'error' event handles errors from the worker, and the 'exit' event fires when the worker stops.
Fill all three blanks to create a map of worker IDs to their last received messages.
const results = {};
workers.forEach(worker => {
worker.on('[1]', (message) => {
results[worker.[2]] = message;
});
});
console.log(results[[3]]);The 'message' event receives data. Each worker has a 'threadId' property used as a key. To access a specific worker's result, use its threadId.