Challenge - 5 Problems
Web Worker Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a heavy computation runs inside an Angular web worker?
Consider an Angular app using a web worker to perform a heavy calculation. What is the main benefit of running this computation inside a web worker?
Attempts:
2 left
💡 Hint
Think about how web workers affect the main UI thread.
✗ Incorrect
Web workers run code on a separate thread, so the UI thread is free to stay responsive. They do not speed up computation by GPU or share variables directly.
📝 Syntax
intermediate1:30remaining
Which Angular syntax correctly creates a web worker?
You want to create a web worker in Angular using the CLI. Which command is correct?
Attempts:
2 left
💡 Hint
The Angular CLI uses 'generate' for scaffolding code.
✗ Incorrect
The correct command to generate a web worker is ng generate web-worker <name>.
🔧 Debug
advanced2:30remaining
Why does this Angular web worker code fail to update the UI?
Given this Angular component code snippet:
Why might the UI not show the updated
worker.onmessage = ({ data }) => {
this.result = data;
};Why might the UI not show the updated
result value?Attempts:
2 left
💡 Hint
Angular needs to know when to update the UI after async events.
✗ Incorrect
Web worker callbacks run outside Angular's zone, so Angular does not detect changes automatically. You must run the update inside NgZone.run() or trigger change detection manually.
❓ state_output
advanced2:00remaining
What is the value of
result after this Angular web worker message?In an Angular component, you have:
The web worker code is:
What is the value of
worker.postMessage(5);
worker.onmessage = ({ data }) => {
this.result = data * 2;
};The web worker code is:
addEventListener('message', ({ data }) => {
postMessage(data + 3);
});What is the value of
result after the message is received?Attempts:
2 left
💡 Hint
Calculate step-by-step: worker adds 3, then component doubles.
✗ Incorrect
The worker receives 5, adds 3 to get 8, sends 8 back. The component doubles 8 to get 16.
🧠 Conceptual
expert3:00remaining
Why should Angular web workers avoid accessing DOM or Angular services?
Which is the main reason Angular web workers should not access the DOM or Angular services directly?
Attempts:
2 left
💡 Hint
Think about what web workers can and cannot do compared to the main thread.
✗ Incorrect
Web workers run in a separate thread and have no access to the DOM or Angular services. They communicate only via messages.