Bird
0
0

You want to receive multiple results from a worker that sends messages repeatedly. Which approach correctly handles this in Node.js?

hard📝 Application Q15 of 15
Node.js - Worker Threads
You want to receive multiple results from a worker that sends messages repeatedly. Which approach correctly handles this in Node.js?
const { Worker } = require('worker_threads');
const worker = new Worker('./worker.js');
// What should you do here to receive all messages?
AUse worker.on('message', (msg) => { console.log(msg); }); to listen continuously
BCall worker.once('message', (msg) => { console.log(msg); }); to listen once
CUse a loop to call worker.postMessage repeatedly
DUse setTimeout to poll worker for messages
Step-by-Step Solution
Solution:
  1. Step 1: Understand event listeners for multiple messages

    Using worker.on('message') listens continuously for all messages sent by the worker.
  2. Step 2: Compare with other options

    once listens only once, loops or polling are unnecessary and inefficient for receiving messages.
  3. Final Answer:

    Use worker.on('message', (msg) => { console.log(msg); }); to listen continuously -> Option A
  4. Quick Check:

    Continuous message listening = A [OK]
Quick Trick: Use worker.on('message') for all messages, not once or polling [OK]
Common Mistakes:
  • Using once instead of on for multiple messages
  • Trying to poll worker instead of event listening
  • Confusing sending messages with receiving them

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes