Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
greenlet
Advanced tools
Readme
Move an async function into its own thread.
A simplified single-function version of workerize, offering the same performance as direct Worker usage.
The name is somewhat of a poor choice, but it was available on npm.
Greenlet only supports browser environments, since it uses Web Workers. For use in a NodeJS environment, Web Workers must be polyfilled using a library like node-webworker.
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
‼️ Important: never call greenlet() dynamically. Doing so creates a new Worker thread for every call:
-const BAD = () => greenlet(x => x)('bad') // creates a new thread on every call
+const fn = greenlet(x => x);
+const GOOD = () => fn('good'); // uses the same thread on every call
Since Greenlets can't rely on surrounding scope anyway, it's best to always create them at the "top" of your module.
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:
import greenlet from 'greenlet'
let getName = greenlet( async username => {
let url = `https://api.github.com/users/${username}`
let res = await fetch(url)
let profile = await res.json()
return profile.name
})
console.log(await getName('developit'))
🔄 Run this example on JSFiddle
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 window.Worker
:
if (window.Worker) {
...
} else {
...
}
If your app has a Content-Security-Policy,
Greenlet requires worker-src data:
and script-src data:
in your config.
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.
FAQs
Move an async function into its own thread.
The npm package greenlet receives a total of 642 weekly downloads. As such, greenlet popularity was classified as not popular.
We found that greenlet demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.