{"id":15415770,"url":"https://github.com/shilangyu/async-worker","last_synced_at":"2025-07-21T07:33:39.403Z","repository":{"id":34900979,"uuid":"181949812","full_name":"shilangyu/async-worker","owner":"shilangyu","description":"Parallel code execution wrapped in promises ⭐","archived":false,"fork":false,"pushed_at":"2025-06-02T06:25:12.000Z","size":746,"stargazers_count":12,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-26T01:46:39.838Z","etag":null,"topics":["async-functions","async-js","async-worker","thread-workers","web-workers"],"latest_commit_sha":null,"homepage":"https://github.shilangyu.dev/async-worker/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shilangyu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-04-17T18:46:44.000Z","updated_at":"2024-08-07T23:24:17.000Z","dependencies_parsed_at":"2024-02-11T22:30:26.803Z","dependency_job_id":"fd054edc-45c6-4265-b04f-93aa1f00178e","html_url":"https://github.com/shilangyu/async-worker","commit_stats":{"total_commits":343,"total_committers":5,"mean_commits":68.6,"dds":0.565597667638484,"last_synced_commit":"4ba586c0fa54f1f0f14a3b8be3b4b6172b335b60"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/shilangyu/async-worker","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shilangyu%2Fasync-worker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shilangyu%2Fasync-worker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shilangyu%2Fasync-worker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shilangyu%2Fasync-worker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shilangyu","download_url":"https://codeload.github.com/shilangyu/async-worker/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shilangyu%2Fasync-worker/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266261311,"owners_count":23901289,"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":["async-functions","async-js","async-worker","thread-workers","web-workers"],"created_at":"2024-10-01T17:09:35.672Z","updated_at":"2025-07-21T07:33:39.362Z","avatar_url":"https://github.com/shilangyu.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![](https://github.com/shilangyu/async-worker/workflows/async-worker/badge.svg)](https://github.com/shilangyu/async-worker/actions)\n[![dependencies Status](https://david-dm.org/shilangyu/async-worker.svg)](https://david-dm.org/shilangyu/async-worker)\n[![Known Vulnerabilities](https://snyk.io/test/github/shilangyu/async-worker/badge.svg?targetFile=package.json)](https://snyk.io/test/github/shilangyu/async-worker?targetFile=package.json)\n[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/shilangyu/async-worker/issues)\n[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php)\n\n\u003cp align=\"center\"\u003e\n\t\u003cimg src=\"./asyncWorker.png\"\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n✨ Enhance your asynchronous web/node development by sprinkling parallel code execution! ✨\n\u003c/p\u003e\n\n- [installing](#installing)\n- [API](#api)\n  - [task](#task)\n  - [cook](#cook)\n  - [track](#track)\n  - [kill](#kill)\n- [common mistakes](#common-mistakes)\n\n[DEMO](https://github.shilangyu.dev/async-worker/)\n\n### installing\n\nInstall from npm:\n\n```sh\nnpm i async-worker\n```\n\nImport:\n\n```ts\nimport AsyncWorker from 'async-worker'\n```\n\nAlternatively use the embedded script tag (`AsyncWorker` will be available globally):\n\n```html\n\u003cscript src=\"https://cdn.jsdelivr.net/npm/async-worker/dist/async-worker.web.js\"\u003e\u003c/script\u003e\n```\n\n### API\n\n#### task\n\nTo perform a long, expensive tasks outside of the main thread use `asyncWorker.task`\n\n```ts\nconst { task } = new AsyncWorker()\n\nconst primes = await task(\n\t(from, to) =\u003e {\n\t\tlet computedPrimes = []\n\t\t// im computing big data that would otherwise block the main thread\n\t\treturn computedPrimes\n\t},\n\t10,\n\t1000000\n)\n```\n\n```ts\nfunction task\u003cT, S extends any[]\u003e(func: (...args: S) =\u003e T, ...args: S): Promise\u003cT\u003e\n```\n\n#### cook\n\nTo cook a function into an asynchronous one use `asyncWorker.cook`\n\n```ts\nconst { cook } = new AsyncWorker()\n\nconst asyncFibo = cook(() =\u003e (n) =\u003e {\n\tif (n \u003c= 0) return NaN\n\n\tlet [first, second] = [0, 1]\n\twhile (--n) [first, second] = [second, first + second]\n\n\treturn first\n})\n\nconst res = await asyncFibo(5)\nconsole.log(`5th fibonacci number is ${res}`)\n```\n\n```ts\nfunction cook\u003cT, S extends any[], U extends any[]\u003e(\n\tfunc: (...args: S) =\u003e (...args: U) =\u003e T,\n\t...args: S\n): (...args: U) =\u003e Promise\u003cT\u003e\n```\n\n#### track\n\nTo track progress of a task use `asyncWorker.track`\n\n```ts\nconst { track } = new AsyncWorker()\n\nconst tracker = track((tick, n) =\u003e {\n\tlet fact = 1\n\tfor (let i = 1; i \u003c= n; i++) {\n\t\tfact *= i\n\t\ttick(i / n)\n\t}\n\treturn fact\n}, 15)\n\ntracker.tick((progress) =\u003e console.log(`the factorial is ${progress * 100}% done!`))\n\nconsole.log(`the result is ${await tracker.result}`)\n```\n\n```ts\nfunction track\u003cT, S extends any[]\u003e(\n\tfunc: (tick: (progress: number) =\u003e void, ...args: S) =\u003e T,\n\t...args: S\n): {\n\tresult: Promise\u003cT\u003e\n\ttick: (ticker: (progress: number) =\u003e void) =\u003e void\n}\n```\n\n#### kill\n\nOnce you're done using, you can kill it\n\n```ts\nconst async = new AsyncWorker()\n\nasync.task(/* doing something */)\n\nasync.kill()\n```\n\n### common mistakes\n\nBecause web workers/worker threads exist in a different thread the passed function does not have access to your current context variables. To pass in variables please add them as additional parameters and accept them in your functions. This will **not** work:\n\n```ts\n//...\nconst from = 10\nconst to = 1000000\nconst primes = await task(() =\u003e {\n\tconsole.log(from) // error during worker runtime: 'from' is not defined\n\tconsole.log(to) // error during worker runtime: 'to' is not defined\n})\n//...\n```\n\nDo instead:\n\n```ts\n//...\nconst from = 10\nconst to = 1000000\nconst primes = await task(\n\t(from, to) =\u003e {\n\t\tconsole.log(from) // :)\n\t\tconsole.log(to) // :)\n\t},\n\tfrom,\n\tto\n)\n//...\n```\n\nSome types are not [transferable](https://developer.mozilla.org/en-US/docs/Web/API/Transferable). Meaning you cannot send them to or receive from a web worker. Notably functions are not transferable. The following will **not** work:\n\n```ts\n//...\nconst answer = () =\u003e 42\nconst result = await task((answer) =\u003e {\n\t// DataCloneError: could not clone '() =\u003e 42'\n}, answer)\n//...\n```\n\nNor will:\n\n```ts\n//...\nconst func = await task(() =\u003e {\n\treturn () =\u003e 42\n\t// DataCloneError: could not clone '() =\u003e 42'\n})\n//...\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshilangyu%2Fasync-worker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshilangyu%2Fasync-worker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshilangyu%2Fasync-worker/lists"}