A Web Worker is a way to run JavaScript code in a separate thread, allowing parallel processing without blocking the main thread.
Click to reveal answer
beginner
How do you create a new Web Worker in Deno?
You create a new Worker by using the Worker constructor with the script URL and options, for example: new Worker(new URL('worker.js', import.meta.url).href, { type: 'module' }).
Click to reveal answer
beginner
How does communication happen between the main thread and a Web Worker?
Communication happens by sending messages using postMessage() and receiving them with onmessage event handlers on both sides.
Click to reveal answer
beginner
Why use Web Workers for parallel processing?
Web Workers let you run heavy or blocking tasks in the background, so the main program stays responsive and smooth.
Click to reveal answer
intermediate
What is the significance of the type: 'module' option when creating a Worker in Deno?
It tells Deno to treat the worker script as an ES module, enabling import/export syntax and modern JavaScript features inside the worker.
Click to reveal answer
How do you send data from the main thread to a Web Worker in Deno?
AUsing <code>transfer()</code> method
BUsing <code>send()</code> method
CUsing <code>emit()</code> method
DUsing <code>postMessage()</code> method
✗ Incorrect
The postMessage() method is used to send messages between the main thread and Web Workers.
Which option is required to create a Worker that supports ES modules in Deno?
A{ mode: 'es' }
B{ scriptType: 'module' }
C{ type: 'module' }
D{ module: true }
✗ Incorrect
The { type: 'module' } option tells Deno to treat the worker script as an ES module.
What happens if you run heavy computation on the main thread without Web Workers?
AThe computation runs in parallel automatically
BThe UI becomes unresponsive
CThe program runs faster
DNothing changes
✗ Incorrect
Heavy tasks block the main thread, making the UI freeze or become unresponsive.
How do you listen for messages from a Web Worker in the main thread?
B. postMessage is called before onmessage, causing error
C. Worker is terminated immediately, so no message is received
D. Missing event listener for error events
Solution
Step 1: Analyze termination timing
The worker is terminated right after sending the message, likely before it can respond.
Step 2: Effect on message reception
Because of immediate termination, the onmessage handler never receives data.
Final Answer:
Worker is terminated immediately, so no message is received -> Option C
Quick Check:
Terminate too soon = no response [OK]
Hint: Terminate workers only after receiving messages [OK]
Common Mistakes:
Thinking postMessage order causes error
Ignoring worker path resolution rules
Forgetting to handle errors but not critical here
5. You want to process a large array of numbers in parallel using Deno Web Workers. Which approach correctly splits the work and collects results efficiently?
hard
A. Process the array in the main thread without workers for simplicity
B. Create one worker and send the entire array repeatedly for each number
C. Use a single worker and process numbers sequentially inside it
D. Create multiple workers, send each a slice of the array, collect partial results, then combine them
Solution
Step 1: Understand parallel processing strategy
Splitting the array into parts and assigning each to a separate worker allows true parallelism.
Step 2: Collect and combine results
Each worker returns partial results; the main thread combines them for the final output.
Final Answer:
Create multiple workers, send each a slice of the array, collect partial results, then combine them -> Option D
Quick Check:
Split work + combine results = efficient parallelism [OK]
Hint: Split data among workers, then merge results [OK]
Common Mistakes:
Sending full array multiple times wastes resources
Using one worker loses parallel benefit
Ignoring main thread processing slows app
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.