Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Receiving Results from Workers in Node.js
📖 Scenario: You are building a Node.js application that uses worker threads to perform tasks in the background. You want to receive results from these workers and handle them in the main thread.
🎯 Goal: Create a simple Node.js program that starts a worker thread, sends a task to it, and receives the result back in the main thread.
📋 What You'll Learn
Create a worker thread using the worker_threads module
Send a message from the main thread to the worker
Receive a message from the worker in the main thread
Log the received result in the main thread
💡 Why This Matters
🌍 Real World
Worker threads help Node.js applications perform CPU-heavy tasks without blocking the main thread, improving responsiveness.
💼 Career
Understanding worker threads is important for backend developers building scalable and efficient Node.js applications that handle concurrent tasks.
Progress0 / 4 steps
1
Set up the worker thread file
Create a file named worker.js that imports parentPort from worker_threads and listens for messages using parentPort.on('message', ...).
Node.js
Hint
Use require('worker_threads') to get parentPort. Then use parentPort.on('message', callback) to listen for messages.
2
Add a task handler in the worker
Inside the parentPort.on('message', ...) callback in worker.js, create a variable result that doubles the received value. Then send the result back to the main thread using parentPort.postMessage(result).
Node.js
Hint
Multiply value by 2 and send it back with parentPort.postMessage().
3
Create the main thread file and start the worker
Create a file named main.js. Import Worker from worker_threads. Create a new Worker instance with the filename './worker.js'. Send the number 10 to the worker using worker.postMessage(10).
Node.js
Hint
Use new Worker('./worker.js') to start the worker and worker.postMessage(10) to send data.
4
Receive and log the result from the worker
In main.js, add a listener for worker.on('message', ...) that receives the result from the worker and logs it using console.log with the text "Result from worker:" followed by the result.
Node.js
Hint
Use worker.on('message', callback) to get the result and console.log to show it.
Practice
(1/5)
1. In Node.js, how do you receive results from a worker thread?
easy
A. By using console.log inside the worker
B. By calling worker.getResult() method
C. By listening to the message event on the worker
D. By reading from a shared file
Solution
Step 1: Understand worker communication
Workers send results back to the main thread using messages.
Step 2: Use the correct event listener
The main thread listens to the message event on the worker to receive data.
Final Answer:
By listening to the message event on the worker -> Option C
Quick Check:
message event = D [OK]
Hint: Remember: workers send data via 'message' events [OK]
Common Mistakes:
Trying to call a non-existent method like getResult()
Expecting console.log output to be received
Reading results from files instead of messages
2. Which of the following is the correct syntax to listen for messages from a worker in Node.js?
easy
A. worker.on('message', (result) => { console.log(result); });
B. worker.listen('message', (result) => { console.log(result); });
C. worker.addEventListener('message', (result) => { console.log(result); });
D. worker.receive('message', (result) => { console.log(result); });
Solution
Step 1: Recall Node.js worker event syntax
Node.js workers use the on method to listen for events.
Step 2: Identify the correct event and method
The event to receive data is message, and the syntax is worker.on('message', callback).
Final Answer:
worker.on('message', (result) => { console.log(result); }); -> Option A
Quick Check:
worker.on('message') = A [OK]
Hint: Use worker.on('message', callback) to get results [OK]
Common Mistakes:
Using non-existent methods like listen or receive
Confusing browser event syntax with Node.js
Using addEventListener which is not in Node.js workers
3. What will be logged to the console when this Node.js worker code runs?