{"id":13393195,"url":"https://github.com/developit/greenlet","last_synced_at":"2025-05-14T04:07:34.945Z","repository":{"id":46143436,"uuid":"118982952","full_name":"developit/greenlet","owner":"developit","description":"🦎 Move an async function into its own thread.","archived":false,"fork":false,"pushed_at":"2021-03-01T20:22:52.000Z","size":56,"stargazers_count":4702,"open_issues_count":15,"forks_count":99,"subscribers_count":43,"default_branch":"master","last_synced_at":"2025-05-04T19:38:40.166Z","etag":null,"topics":["thread","web-worker","webworker","worker","workerize"],"latest_commit_sha":null,"homepage":"https://npm.im/greenlet","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/developit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-26T00:11:57.000Z","updated_at":"2025-05-01T06:29:06.000Z","dependencies_parsed_at":"2022-09-26T16:21:49.166Z","dependency_job_id":null,"html_url":"https://github.com/developit/greenlet","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fgreenlet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fgreenlet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fgreenlet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developit%2Fgreenlet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/developit","download_url":"https://codeload.github.com/developit/greenlet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252580112,"owners_count":21771265,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["thread","web-worker","webworker","worker","workerize"],"created_at":"2024-07-30T17:00:45.964Z","updated_at":"2025-05-14T04:07:34.858Z","avatar_url":"https://github.com/developit.png","language":"JavaScript","readme":"\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://i.imgur.com/e8XbYbd.png\" width=\"1000\" alt=\"Greenlet\"\u003e\n\u003c/p\u003e\n\n## Greenlet [![npm](https://img.shields.io/npm/v/greenlet.svg)](https://npm.im/greenlet) [![travis](https://travis-ci.org/developit/greenlet.svg?branch=master)](https://travis-ci.org/developit/greenlet) [![gzip size](http://img.badgesize.io/https://unpkg.com/greenlet/dist/greenlet.js?compression=gzip)](https://unpkg.com/greenlet/dist/greenlet.umd.js) [![install size](https://packagephobia.now.sh/badge?p=greenlet)](https://packagephobia.now.sh/result?p=greenlet)\n\n\u003e Move an async function into its own thread.\n\u003e\n\u003e A simplified single-function version of [workerize](https://github.com/developit/workerize), offering [the same performance as direct Worker usage](https://esbench.com/bench/5b16b61af2949800a0f61ce3).\n\nThe name is somewhat of a poor choice, but it was [available on npm](https://npm.im/greenlet).\n\n_Greenlet supports IE10+, since it uses [Web Workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers). For NodeJS usage, Web Workers must be polyfilled using a library like [node-webworker](https://github.com/pgriess/node-webworker)._\n\n## Installation \u0026 Usage\n\n```sh\nnpm i -S greenlet\n```\n\nAccepts an async function with, produces a copy of it that runs within a Web Worker.\n\n\u003e ⚠️ **Caveat:** the function you pass cannot rely on its surrounding scope, since it is executed in an isolated context.\n\n```\ngreenlet(Function) -\u003e Function\n```\n\n\u003e ‼️ **Important:** never call greenlet() dynamically. Doing so creates a new Worker thread for every call:\n\n```diff\n-const BAD = () =\u003e greenlet(x =\u003e x)('bad') // creates a new thread on every call\n+const fn = greenlet(x =\u003e x);\n+const GOOD = () =\u003e fn('good'); // uses the same thread on every call\n```\n\nSince Greenlets can't rely on surrounding scope anyway, it's best to always create them at the \"top\" of your module.\n\n\n## Example\n\nGreenlet is most effective when the work being done has relatively small inputs/outputs.\n\nOne such example would be fetching a network resource when only a subset of the resulting information is needed:\n\n```js\nimport greenlet from 'greenlet'\n\nlet getName = greenlet( async username =\u003e {\n    let url = `https://api.github.com/users/${username}`\n    let res = await fetch(url)\n    let profile = await res.json()\n    return profile.name\n})\n\nconsole.log(await getName('developit'))\n```\n\n[🔄 **Run this example on JSFiddle**](https://jsfiddle.net/developit/mf9fbma5/)\n\n\n## Transferable ready\n\nGreenlet will even accept and optimize [transferables](https://developer.mozilla.org/en-US/docs/Web/API/Transferable) as arguments to and from a greenlet worker function.\n\n\n## Browser support\n\nThankfully, Web Workers have been around for a while and [are broadly supported](https://caniuse.com/#feat=webworkers) by Chrome, Firefox, Safari, Edge, and Internet Explorer 10+.\n\nIf you still need to support older browsers, you can just check for the presence of `window.Worker`:\n\n```js\nif (window.Worker) {\n    ...\n} else {\n    ...\n}\n```\n\n### CSP\n\nIf your app has a [Content-Security-Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy), Greenlet requires `worker-src blob:` and `script-src blob:` in your config.\n\n## License \u0026 Credits\n\n\u003e In addition to the contributors, credit goes to [@sgb-io](https://github.com/sgb-io) for his annotated exploration of Greenlet's source. This prompted a refactor that clarified the code and allowed for further size optimizations.\n\n[MIT License](https://oss.ninja/mit/developit)\n","funding_links":[],"categories":["Web Worker","JavaScript","*.js","others","Framework agnostic packages"],"sub_categories":["Browser","Runner","Docker Custom Builds","Web workers","Toolkit"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelopit%2Fgreenlet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevelopit%2Fgreenlet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevelopit%2Fgreenlet/lists"}