Last active
December 10, 2019 06:48
-
-
Save Trott/c902a350de3fc8112a2227d02e7de49e to your computer and use it in GitHub Desktop.
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
const { Worker, isMainThread, parentPort } = require('worker_threads'); | |
if (isMainThread) { | |
// This code is executed in the main thread and not in the worker. | |
// Create the worker. | |
const worker = new Worker(__filename); | |
// Listen for messages from the worker and print them. | |
worker.on('message', (msg) => { console.log(msg); }); | |
} else { | |
// This code is executed in the worker and not in the main thread. | |
// Send a message to the main thread. | |
parentPort.postMessage('Hello world!'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment