Last active
February 4, 2020 14:53
-
-
Save leon0707/39f1cf54bf102dac05153afc18321be3 to your computer and use it in GitHub Desktop.
a worker script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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