Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
Node.js - Worker Threads
Identify the error in this code snippet:
const { Worker } = require('worker_threads');
const worker = new Worker();
worker.postMessage('Hi');
AMissing filename argument when creating Worker
BpostMessage() cannot be called on Worker instance
CWorker class is not imported correctly
DpostMessage() requires a callback function
Step-by-Step Solution
Solution:
  1. Step 1: Check Worker constructor usage

    The Worker constructor requires a filename or code string argument to run.
  2. Step 2: Identify missing argument

    Here, new Worker() is called without any argument, causing an error.
  3. Final Answer:

    Missing filename argument when creating Worker -> Option A
  4. Quick Check:

    Worker needs filename argument in constructor [OK]
Quick Trick: Always provide filename when creating Worker [OK]
Common Mistakes:
  • Thinking postMessage() is invalid on Worker
  • Assuming Worker import is wrong
  • Believing postMessage() needs a callback

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes