Recall & Review
beginner
What is the main way to send data to a worker thread in Node.js?
You send data to a worker thread by passing it as an argument when creating the Worker or by using the
postMessage() method to send messages after the worker starts.Click to reveal answer
beginner
How does the worker thread receive data sent from the main thread?
The worker thread listens for messages using the
parentPort.on('message', callback) event handler to receive data sent from the main thread.Click to reveal answer
intermediate
Can you pass complex objects like functions or class instances directly to a worker thread?No, you cannot pass functions or class instances directly. Only data that can be serialized (like objects, arrays, strings, numbers) can be passed between threads.Click to reveal answer
beginner
What is the benefit of using
workerData when creating a new Worker?workerData allows you to send initial data to the worker thread at creation time, so the worker can start working immediately with that data.Click to reveal answer
beginner
How do you send data back from a worker thread to the main thread?
The worker thread uses
parentPort.postMessage() to send data back to the main thread, which listens with worker.on('message').Click to reveal answer
Which method sends data from the main thread to a worker after it has started?
✗ Incorrect
The
postMessage() method sends data to a worker after it has started. workerData is only for initial data.Where does a worker thread listen for incoming messages from the main thread?
✗ Incorrect
The worker thread listens on
parentPort.on('message') to receive messages from the main thread.What kind of data can be passed between the main thread and worker threads?
✗ Incorrect
Only serializable data such as objects, arrays, strings, and numbers can be passed between threads.
How do you pass initial data to a worker thread when creating it?
✗ Incorrect
Initial data is passed to a worker thread via the
workerData option in the Worker constructor.How does the main thread receive data sent from a worker thread?
✗ Incorrect
The main thread listens for messages from a worker using
worker.on('message').Explain how data is passed from the main thread to a worker thread and how the worker receives it.
Think about the two ways to send data: at creation and after creation.
You got /3 concepts.
Describe the limitations on the types of data you can pass between the main thread and worker threads in Node.js.
Consider what can be converted to a message safely.
You got /3 concepts.