Changes

Jump to: navigation, search

DPS921/Web Worker API

875 bytes added, 03:36, 5 December 2020
Error Handling
=== Error Handling ===
Web Worker API provides the capability of handling errors in the worker processes from the main process. When an Error is raised in a worker, an ErrorEvent will be fired to the main process. We can listen to this event by assigning a function to the event handler onerror of the worker object.
The following code sets up the main process to print to the console a message when an Error is raised in the worker:
 
<pre>
let worker = new Worker('worker.js');
worker.onerror = function(e) {
console.log(`An error occcured in the ${e.filename} worker at line %{e.lineno}`);
console.log(`Error: ${e.message}`);
}
</pre>
 
Note that the ErrorEvent has 3 special properties which are filename (the name of the worker's source file where the error occured), lineno (the line number in the worker's source file where the error occured) and message (a description of the error).
== Other Workers ==

Navigation menu