Complete the code to create a new worker thread.
const { Worker } = require('worker_threads');
const worker = new [1]('./worker.js');In Node.js, Worker is used to create a new worker thread.
Complete the code to spawn a child process using Node.js.
const { spawn } = require('child_process');
const child = spawn('[1]', ['-lh', '/usr']);The spawn function runs a command; here, ls lists directory contents.
Fix the error in the worker thread message listener.
worker.on('message', ([1]) => { console.log('Received:', data); });
The parameter name must match the variable used inside the function. Here, data is used, so the parameter should be data.
Fill both blanks to send a message from the main thread to the worker.
worker.[1]({ text: 'Hello' }); worker.[2]('exit', () => console.log('Worker exited'));
Use postMessage to send data to a worker thread, and on to listen for events like 'exit'.
Fill all three blanks to create a child process and handle its output.
const { [1] } = require('child_process');
const child = [2]('node', ['script.js']);
child.stdout.on('[3]', data => console.log(`Output: ${data}`));Import spawn from 'child_process', use it to create the child process, and listen for 'data' events on stdout.
