Creating a worker
Before you can create a worker, you need a JavaScript file with code that runs in the worker thread. You can see a simple example of a worker definition file at https://github.com/mdn/simple-web-worker/blob/gh-pages/worker.js. The file should contain a message event listener that performs operations when messages are received from other threads and responds accordingly.
Once that file is created, you're ready to use it with a worker. A worker is created by passing a URL argument to the Worker() constructor. The URL can be a string representing the name of the file with your worker definition code, or constructed using a Blob. The Blob technique can be useful if you're fetching the worker definition code from a server. The ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access