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:
import { Worker } from 'worker_threads';
const worker = new Worker('./worker.js', { data: { num: 5 } });
AMissing import of workerData in main thread
BThe path './worker.js' is incorrect
CWorker constructor requires a callback function
DThe option 'data' should be 'workerData'
Step-by-Step Solution
Solution:
  1. Step 1: Check option names in Worker constructor

    The correct option to pass data is 'workerData', not 'data'.
  2. Step 2: Validate other parts

    The path can be correct, Worker constructor does not require a callback, and workerData is imported in worker, not main thread.
  3. Final Answer:

    The option 'data' should be 'workerData' -> Option D
  4. Quick Check:

    Use workerData option to pass data [OK]
Quick Trick: Use workerData, not data, to pass initial data [OK]
Common Mistakes:
  • Using wrong option name 'data'
  • Confusing main thread imports with worker imports

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes