daily pastebin goal
95%
SHARE
TWEET

Untitled

a guest Mar 21st, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { Worker, isMainThread } = require("worker_threads");
  2.  
  3. function runService(workerData) {
  4.   const worker = new Worker("./service.js", { workerData });
  5.   worker.postMessage("once");
  6.   worker.on("message", incoming => console.log({ incoming }));
  7.   worker.on("error", code => new Error(`Worker error with exit code ${code}`));
  8.   worker.on("exit", code =>
  9.     console.log(`Worker stopped with exit code ${code}`)
  10.   );
  11.   worker.postMessage("twice");
  12.   worker.postMessage("three times");
  13.   worker.postMessage("exit");
  14.   setTimeout(() => worker.postMessage("you won't see me"), 100);
  15. }
  16.  
  17. async function run() {
  18.   const result = runService("let's begin");
  19.   console.log({ isMainThread });
  20. }
  21.  
  22. run().catch(err => console.error(err));
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top