Skip to content

mdubourg001/node-routine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Routine

Inline, Promise based wrapper around JS Web Workers

GitHub npm

yarn add worker-routine

Usage

Using async / await:

import routine from "worker-routine";

const inneficientSquare = n => {
  let total = 0;
  for (let i = 0; i < n; i++) {
    for (let j = 0; j < n; j++) {
      total++;
    }
  }
  return total;
};

// spawns two workers running in separate threads
const results = await Promise.all([
  routine(inneficientSquare, 80000),
  routine(inneficientSquare, 80001)
]);

console.log(results[0] * results[1]);

Using Promise.then:

import routine from "worker-routine";

const multiply = (a, b, c) => a * b * c;

routine(multiply, 2, 3, 10).then(
  console.log // => 60
);

⚠️ routine isn't magic, it is just a wrapper around Web Worker. Every limitations you can encounter with Web Workers, you will encounter with routine:

  • functions passed to routine must be pure: they must not have any side-effects: no fetch calls, no references to global variables, etc...

About

Inline, Promise based wrapper around JS Web Workers.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published