- Lab
- Core Tech

Guided: Working with Web Workers
Ready to boost your JavaScript skills? In this Working with Web Workers code lab, you’ll build a number-crunching web application using Web Workers to perform heavy computations in the background. You’ll learn to create and manage Web Workers, communicate between the main thread and workers, and optimize performance for a smooth user experience. Dive in and master Web Workers with hands-on tasks to create a responsive web app!

Path Info
Table of Contents
-
Challenge
### Introduction to Web Workers
Welcome to this Working with Web Workers lab, where you’ll build a number-crunching web app!
You’ll use Web Workers to perform heavy calculations, like generating Fibonacci sequences, in a background thread, ensuring the UI remains responsive. You’ll learn to create Web Workers, communicate with them using
postMessage()
, and manage their lifecycle.The goal is to create a performant app that handles intensive tasks efficiently, equipping you with skills for real-world web development.
Web Workers are essential for modern web applications, allowing you to offload heavy tasks to background threads.
Without Web Workers, complex calculations can freeze the UI, frustrating users. In this app, a Web Worker will compute Fibonacci numbers while the main thread keeps the UI interactive, demonstrating the power of parallel processing in the browser.
Communication between threads and proper worker management are critical for reliable apps. You’ll use
postMessage()
andonmessage
to exchange data and handle worker errors to ensure smooth operation.These skills will help you build responsive, high-performance web applications, starting with this number-crunching app.
info> If you get stuck, you can reference the solutions folder containing the full code solution for this lab! You can also view the latest code in the Web Browser tab by clicking the Refresh button.
-
Challenge
### Setting Up the Web Worker Environment
You’ll begin by setting up your project to use a Web Worker.
This step focuses on creating the main HTML file, JavaScript for the main thread, and adding a separate worker script. These tasks lay the foundation for your Web Worker–based application. Web Workers require a separate script file to define their logic. This separation keeps the main thread free to handle UI tasks. Creating an HTML file with a simple UI sets the stage for user interaction. The main thread is responsible for loading and controlling the Web Worker. Setting up a separate JavaScript file for the main thread helps organizes your logic clearly. The Web Worker script (
worker.js
) defines the background thread’s logic. A placeholderonmessage
handler sets up the worker to receive messages, preparing it for computations. -
Challenge
### Creating and Initializing the Web Worker
Now you’ll create and initialize the Web Worker to perform calculations.
This step focuses on instantiating a Web Worker in the main thread and setting up the initial communication. Creating a Web Worker is the foundation for running tasks in the background. Instantiating a Web Worker connects the main thread to the worker script, enabling background computations. This is the first step in delegating tasks to a separate thread.
You create a Web Worker by using the
Worker
constructor and storing the instance in a variable. The main thread must process results from the Web Worker. Inmain.js
, you can attach anonmessage
event listener to the worker to handle the results and update the UI. Use this handler to log the data and display it in the app. -
Challenge
### Implementing Messaging and Computation
Next, you’ll implement messaging between the main thread and the Web Worker to perform Fibonacci calculations.
This step focuses on using
postMessage()
to send inputs and receive results, and defining the worker’s computation logic. Messaging is key to coordinating tasks across threads. Sending user input to the Web Worker via a button click enables the app to trigger computations. This connects the UI to the worker’s logic.You can add a click event listener to the button to read the input value and send it to the worker using
postMessage()
. The Web Worker must perform the Fibonacci computation to offload work from the main thread. This implements the core functionality of the app.You can update the
onmessage
handler inworker.js
to compute the Fibonacci sequence and send the result back. -
Challenge
### Managing the Web Worker and Handling Errors
You’ll now enhance your app by managing the Web Worker’s lifecycle and adding error handling.
This step focuses on terminating the worker when needed and catching errors from the worker. Proper management ensures efficient resource use and a robust user experience. Terminating the Web Worker frees up resources and improves efficiency. A Stop button gives users control over the worker’s lifecycle.
You can add a Stop button to
index.html
, and inmain.js
, aclick
event listener that callsworker.terminate()
. Validating inputs in the worker prevents errors during computation, improving reliability. Sending error messages informs the main thread of issues.You can add input validation in
worker.js
’sonmessage
handler and send error messages viapostMessage()
. Catching worker errors in the main thread ensures that users are informed of issues such as script errors and enhances the user experience.To complete the app's error handling, add an
onerror
handler to theworker
that displays the error message in theresult
div
. -
Challenge
### Conclusion and Next Steps
Great job—you’ve built a number-crunching app with Web Workers!
You’ve completed a practical project that demonstrates how to use Web Workers to perform heavy computations without freezing the UI. This lab has equipped you with valuable skills for building performant web applications.
You’ve learned how to create Web Workers to run tasks in the background, keeping the UI responsive. With messaging, you used
postMessage()
andonmessage
to communicate between threads. Managing workers and handling errors ensured your app is efficient and user-friendly. These skills are essential for optimizing real-world web applications.Now, consider use cases where Web Workers shine, such as processing large datasets (for example image filtering or data analysis) or running simulations. To enhance this app, try adding a progress indicator by sending intermediate results from the worker. You could also experiment with multiple workers for parallel tasks. For a challenge, try implementing a Web Worker to handle real-time data processing, like streaming JSON data.
I hope this lab inspires you to explore more JavaScript video courses and code labs to continue your learning journey here, at Pluralsight!
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.