Setting up a shared worker

A shared worker can be created by calling the SharedWorker constructor and providing the name of the file as the argument:

const awesomeworker = new SharedWorker('myworker.js');

Here, we used the SharedWorker constructor to create an instance of a sharedworker object. Unlike with dedicated workers, you won't be able to see the HTTP network request in the browser made to the myworker.js file. This is important because the browser has to maintain only one instance of this file across multiple scripts calling this web worker:

// myworker.jsconsole.log('Hello world!');

Unlike dedicated workers, this does not log Hello World! in the main website's console. This is because shared workers do not get loaded into only that ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.