Move an async function into its own thread.
A simplified single-function version of workerize.
The name is somewhat of a poor choice, but it was available on npm.
Installation & Usage
npm i -S greenlet
Accepts an async function with, produces a copy of it that runs within a Web Worker.
⚠️ Caveat: the function you pass cannot rely on its surrounding scope, since it is executed in an isolated context.
greenlet(Function) -> Function
Greenlet is most effective when the work being done has relatively small inputs/outputs.
One such example would be fetching a network resource when only a subset of the resulting information is needed:
let getName =console
Greenlet will even accept and optimize transferables as arguments to and from a greenlet worker function.
Thankfully, Web Workers have been around for a while and are broadly supported by Chrome, Firefox, Safari, Edge, and Internet Explorer 10+.
If you still need to support older browsers, you can just check for the presence of
If your app has a Content-Security-Policy,
worker-src data: and
script-src data: in your config.
License & Credits
In addition to the contributors, credit goes to @sgb-io for his annotated exploration of Greenlet's source. This prompted a refactor that clarified the code and allowed for further size optimizations.