spawn-worker

Spawn a web worker with a function

Usage no npm install needed!

<script type="module">
  import spawnWorker from 'https://cdn.skypack.dev/spawn-worker';
</script>

README

spawn-worker

Spawn a web worker with a function

Install

$ npm i spawn-worker

Usage

const spawn = require('spawn-worker');

// Single worker
const worker = spawn(function() {
  postMessage('Yo!');
  self.close();
});

worker.onmessage = function(event) {
  console.log(event.data); // 'Yo!'
};

// Multiple workers
const workers = spawn(function() {
  postMessage('Yo!');
  self.close();
}, 2);

workers.forEach(function(worker) {
  worker.onmessage = function(event) {
    console.log(event.data); // 'Yo!'
  };
});

API

spawn(function[, count])

  • function Function to be executed inside of a worker
  • count Number of workers, Optional, Default: 1
  • Return: single or multiple instances of Worker

License

MIT