Web Worker API methods and properties

Methods

postMessage()

The postMessage() method allows communication between the main thread and the web worker thread. It sends data from the main thread to the worker or vice versa. It takes a single parameter, the message payload, which can be any serializable object.

// Main thread
const worker = new Worker('worker.js');
worker.postMessage({ data: 'Hello from the main thread' });

// Worker thread (worker.js)
self.onmessage = function(event) {
  const { data } = event;
  console.log(data); // Output: { data: 'Hello from the main thread' }
};

terminate()

The terminate() method stops the execution of a web worker. When called, it immediately terminates the worker thread, terminating any ongoing tasks. It is useful when you no longer need a worker and want to free up resources.

// Main thread
const worker = new Worker('worker.js');
worker.terminate();

// Worker thread (worker.js)
// ... Worker code will not execute if terminated ...

Properties

onmessage

The onmessage property is an event handler that triggers whenever a message is received from the main thread. You can define a callback function to handle incoming messages.

// Worker thread (worker.js)
self.onmessage = function(event) {
  const { data } = event;
  console.log(data); // Output: { data: 'Hello from the main thread' }
};

onerror

The onerror property is an event handler that triggers when an error occurs in the worker thread. It allows you to handle and log errors that occur during the execution of the worker.

// Worker thread (worker.js)
self.onerror = function(error) {
  console.error(error);
};

These are just a few of the essential methods and properties provided by the Web Worker API to leverage the full potential of background JavaScript processing. By utilizing these features, developers can enhance the responsiveness and performance of their web applications.

#WebDevelopment #JavaScript