Skip to content

Instantly share code, notes, and snippets.

@leon0707
Last active February 4, 2020 14:53
Show Gist options
  • Select an option

  • Save leon0707/39f1cf54bf102dac05153afc18321be3 to your computer and use it in GitHub Desktop.

Select an option

Save leon0707/39f1cf54bf102dac05153afc18321be3 to your computer and use it in GitHub Desktop.
a worker script
// web-worker.js
self.addEventListener('message', (event) => {
console.log('worker receives a msg');
if (event.data === 'close your thread') {
console.log('close msg received');
try {
self.close();
// this thread is terminated.
} catch (error) {
self.postMessage('cannot terminated by itself');
}
} else if (event.data === 'throw an error') {
// throw an error
throw new TypeError('error handler in the main thread will handle this');
} else {
self.postMessage('msg received, ack');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment