Bird
0
0

How do you correctly instantiate a new worker thread in Node.js using the worker_threads module?

easy📝 Syntax Q3 of 15
Node.js - Worker Threads
How do you correctly instantiate a new worker thread in Node.js using the worker_threads module?
Aconst worker = new Worker();
Bconst worker = new Worker('console.log("Hello")');
Cconst worker = new Worker('./worker.js');
Dconst worker = new Worker(require('worker_threads'));
Step-by-Step Solution
Solution:
  1. Step 1: Import Worker

    Use const { Worker } from worker_threads module.
  2. Step 2: Provide a valid file path

    The Worker constructor requires a path to a JavaScript file to run in the worker thread.
  3. Final Answer:

    const worker = new Worker('./worker.js'); is correct (const worker = new Worker('./worker.js');).
  4. Quick Check:

    Worker needs a file path, not inline code or empty constructor. [OK]
Quick Trick: Worker needs a file path, not inline code. [OK]
Common Mistakes:
  • Passing inline code string instead of file path
  • Calling Worker without arguments
  • Passing the module itself instead of a file path

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes