Errors in webworker-threads thread are not printed to console by default, thus using ‘try-catch’ is one of the way to show these stuff.

Example code:

var worker = require("webworker-threads").Worker;

var Thread1 = new worker(function(){
  console.log("Thread started");
  this.onmessage = function(Event) {
    try {
      //assign to undefined variable
      some_variable = 0;
    }
    catch (Error) {
      //this console.log is a helper in the thread with the same
      //name with default JavaScript console.log.
      //printing 'Error' won't show anything.
      //printing 'Error.stack' shows the full stack trace
      console.log(Error.stack);
    }
  };
});

Thread1.postMessage("");
Advertisements