0
0
Node.jsframework~10 mins

Receiving results from workers in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Receiving results from workers
Main thread starts worker
Worker runs task
Worker sends result message
Main thread receives message
Main thread processes result
End
The main thread starts a worker, the worker runs a task and sends back a message, then the main thread receives and processes the result.
Execution Sample
Node.js
import { Worker } from 'worker_threads';

const worker = new Worker('./worker.js');
worker.on('message', (result) => {
  console.log('Result:', result);
});
Main thread creates a worker and listens for messages containing results.
Execution Table
StepActionWorker StateMain Thread StateMessage/Event
1Main thread creates workerIdleWorker createdNo message
2Worker starts taskRunning taskWaiting for messageNo message
3Worker finishes taskTask completeWaiting for messageNo message
4Worker sends result messageSent messageMessage event queuedMessage with result sent
5Main thread receives messageIdleMessage receivedMessage event handled
6Main thread processes resultIdleResult processedConsole logs result
7EndIdleIdleNo message
💡 Worker sent result message and main thread processed it, communication complete.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5Final
workerStateIdleRunning taskSent messageIdleIdle
mainThreadStateWorker createdWaiting for messageMessage event queuedMessage receivedResult processed
messageNoneNoneResult message sentResult message receivedProcessed
Key Moments - 3 Insights
Why does the main thread wait before receiving the worker's message?
Because the worker must finish its task and send a message first (see steps 2 to 4 in execution_table). The main thread listens but does not get the message until the worker sends it.
What happens if the worker sends multiple messages?
The main thread's 'message' event handler will be called for each message received, processing them one by one as they arrive.
Is the workerState 'Idle' immediately after sending the message?
No, the worker is 'Sent message' at step 4, then becomes 'Idle' after the main thread receives the message (step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the workerState at step 3?
ATask complete
BRunning task
CIdle
DSent message
💡 Hint
Check the 'Worker State' column at step 3 in the execution_table.
At which step does the main thread receive the worker's message?
AStep 4
BStep 5
CStep 6
DStep 3
💡 Hint
Look for 'Message received' in the 'Main Thread State' column.
If the worker never sends a message, what will the main thread state be after step 4?
AMessage event queued
BResult processed
CWaiting for message
DWorker created
💡 Hint
Refer to the 'Main Thread State' column and consider what happens if no message arrives.
Concept Snapshot
Receiving results from workers in Node.js:
- Main thread creates a Worker instance.
- Worker runs code and sends messages with parentPort.postMessage().
- Main thread listens with worker.on('message', callback).
- Messages carry results from worker to main thread.
- Main thread processes results inside the message event handler.
Full Transcript
In Node.js, the main thread creates a worker thread to run code separately. The worker performs a task and sends results back using messages. The main thread listens for these messages with an event handler. When the worker sends a message, the main thread receives it and processes the result. This allows parallel work and communication between threads.