Workerize

Run a module in a Web Worker.

README

💖 Using Webpack? You want workerize-loader ➡️

workerize

Workerize npm travis


Moves a module into a Web Worker, automatically reflecting exported functions as asynchronous proxies.


- Bundles a tiny, purpose-built RPC implementation into your app
- If exported module methods are already async, signature is unchanged
- Supports synchronous and asynchronous worker functions
- Works beautifully with async/await
- Just 800 bytes of gzipped ES3


Install


  1. ```sh
  2. npm install --save workerize
  3. ```


Usage


Pass either a function or a string containing code.

worker.js:

  1. ``` js
  2. let worker = workerize(`
  3. export function add(a, b) {
  4.   // block for half a second to demonstrate asynchronicity
  5.   let start = Date.now();
  6.   while (Date.now()-start < 500);
  7.   return a + b;
  8. }
  9. `);

  10. (async () => {
  11. console.log('3 + 9 = ', await worker.add(3, 9));
  12. console.log('1 + 2 = ', await worker.add(1, 2));
  13. })();
  14. ```

License