📖 Scenario: You are building a Deno application that needs to perform a heavy calculation without freezing the main program. To do this, you will use Web Workers to run the calculation in parallel.
🎯 Goal: Create a Deno program that uses a Web Worker to calculate the sum of numbers from 1 to 1,000,000 without blocking the main thread.
📋 What You'll Learn
Create a Web Worker script file with the calculation logic
Create a main Deno script that starts the Worker
Send a message from the main script to the Worker to start calculation
Receive the result from the Worker and display it in the main script
💡 Why This Matters
🌍 Real World
Web Workers allow web and Deno applications to perform heavy tasks like calculations or data processing in the background. This keeps the app responsive and smooth for users.
💼 Career
Understanding Web Workers is important for developers building high-performance web apps or server-side scripts that need parallel processing without freezing the main program.
Progress0 / 4 steps
1
Create the Worker script with calculation logic
Create a file named worker.ts that listens for messages. When it receives a message, calculate the sum of numbers from 1 to 1,000,000 using a for loop and post the result back using self.postMessage.
Deno
Hint
Use self.onmessage to listen for messages and self.postMessage to send results back.
2
Create the main script and start the Worker
In a file named main.ts, create a new Worker instance that loads worker.ts. Store this Worker in a variable called worker.
Deno
Hint
Use new Worker(new URL('./worker.ts', import.meta.url).href, { type: 'module' }) to create the Worker.
3
Send a message to the Worker to start calculation
In main.ts, send a message to the worker using worker.postMessage to trigger the calculation.
Deno
Hint
Use worker.postMessage('start') to send a message to the Worker.
4
Receive and display the result from the Worker
In main.ts, add an event listener for worker.onmessage. When a message is received, assign the data to a variable called result and log it to the console.
Deno
Hint
Use worker.onmessage = (event) => { result = event.data; console.log(result); } to get and display the result.
Practice
(1/5)
1. What is the main purpose of using Web Workers in Deno?
easy
A. To create a new HTTP server
B. To run tasks in parallel without blocking the main thread
C. To manage file system permissions
D. To handle database connections
Solution
Step 1: Understand Web Workers role
Web Workers allow running scripts in background threads separate from the main thread.
Step 2: Identify the benefit
This prevents the main thread from freezing during heavy tasks, enabling parallel processing.
Final Answer:
To run tasks in parallel without blocking the main thread -> Option B
Quick Check:
Web Workers = Parallel tasks without blocking [OK]
Hint: Web Workers run code in background threads [OK]
Common Mistakes:
Thinking Web Workers handle HTTP or database tasks directly
Confusing Web Workers with main thread code
Assuming Web Workers manage permissions
2. Which of the following is the correct way to send a message from the main thread to a Web Worker in Deno?
easy
A. worker.postMessage('Hello')
B. worker.emit('Hello')
C. worker.send('Hello')
D. worker.message('Hello')
Solution
Step 1: Recall message sending method
In Web Workers, the method to send data to the worker is postMessage.
Step 2: Match correct syntax
Among the options, only worker.postMessage('Hello') is valid and recognized.
Final Answer:
worker.postMessage('Hello') -> Option A
Quick Check:
Send message = postMessage [OK]
Hint: Use postMessage to send data to workers [OK]
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.