How to do it...

  1. Open your command-line application and navigate to your workspace.
  2. Create a new folder named 05-04-stop-workers-with-terminate.
  3. Copy or create an index.html that loads and runs a main function from main.js.
  1. Create a main.js with a function named onMessage that takes a message and workers argument, logs out the type and timeout properties of message.data, and calls terminate on all the workers:
// main.js 
function onComplete(message, workers) { 
  const { index, timeout } = message.data; 
  workers.map((w) => w.terminate()); 
 
  console.log( 
    'Result from worker (%s) after timeout (%s): %s', 
    index, 
    timeout 
  ); 
}  
  1. Create a main function that creates multiple workers, sets the onMessage property with a function that passes all the ...

Get ECMAScript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.