Error handling

Here, error handling is a bit tricky. Since the error can occur anywhere in the script by any port (any parent file), you have to manually send the error to every port. But for that, you'll have to store the ports, as well (when they're connected). Here's how it should look:

// myworker.jsconst ports = [];addEventListener('connect', e => {    const port = e.ports[0];    ports.push(port); // assemble all connections    port.start();    // .. other info});addEventListener('error', e => {    console.log(e); // Info about error    ports.forEach(port => port.postMessage({type: 'error', res: e}));});

Here you can see that we are manually sending the error information to every parent file. Thereafter, you can handle the error in the parent file itself. ...

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.