Bird
0
0

Given this Angular Web Worker code snippet, what will be logged in the console?

medium📝 component behavior Q13 of 15
Angular - Performance Optimization
Given this Angular Web Worker code snippet, what will be logged in the console?
const worker = new Worker(new URL('./compute.worker', import.meta.url));
worker.onmessage = ({ data }) => console.log('Result:', data);
worker.postMessage(10);

Assuming the worker script doubles the input number and sends it back.
AResult: undefined
BResult: 10
CResult: 20
DError: Worker not found
Step-by-Step Solution
Solution:
  1. Step 1: Understand the message flow

    The main thread sends 10 to the worker using postMessage(10).
  2. Step 2: Worker doubles the input and sends back

    The worker processes 10, doubles it to 20, and sends it back via postMessage.
  3. Step 3: Main thread logs the received data

    The onmessage handler logs 'Result: 20'.
  4. Final Answer:

    Result: 20 -> Option C
  5. Quick Check:

    Input 10 doubled = 20 [OK]
Quick Trick: Worker doubles input, so output is input x 2 [OK]
Common Mistakes:
  • Assuming worker returns input unchanged
  • Expecting undefined because of async timing
  • Thinking worker script is missing causing error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes