{"id":18486926,"url":"https://github.com/nairihar/funthreads","last_synced_at":"2025-07-09T16:37:27.679Z","repository":{"id":42506272,"uuid":"143541670","full_name":"nairihar/funthreads","owner":"nairihar","description":"🧵  A lightweight tool built on top of worker_threads, enabling multithreading.","archived":false,"fork":false,"pushed_at":"2023-10-18T21:28:57.000Z","size":830,"stargazers_count":61,"open_issues_count":0,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-06-17T06:05:48.083Z","etag":null,"topics":["multithreading","nodejs","thread","worker","worker-threads"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/nairihar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","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}},"created_at":"2018-08-04T15:41:59.000Z","updated_at":"2024-09-11T22:28:52.000Z","dependencies_parsed_at":"2023-02-06T15:46:13.341Z","dependency_job_id":"79ea11df-fce9-4121-b3ac-7d0990060176","html_url":"https://github.com/nairihar/funthreads","commit_stats":{"total_commits":42,"total_committers":5,"mean_commits":8.4,"dds":0.4285714285714286,"last_synced_commit":"cdf1b12860414d164cc031e2f8a25d6310d8be47"},"previous_names":["nairihar/function-threads"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/nairihar/funthreads","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nairihar%2Ffunthreads","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nairihar%2Ffunthreads/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nairihar%2Ffunthreads/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nairihar%2Ffunthreads/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nairihar","download_url":"https://codeload.github.com/nairihar/funthreads/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nairihar%2Ffunthreads/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262387866,"owners_count":23303314,"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":["multithreading","nodejs","thread","worker","worker-threads"],"created_at":"2024-11-06T12:49:57.968Z","updated_at":"2025-07-09T16:37:27.594Z","avatar_url":"https://github.com/nairihar.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"![](https://img.shields.io/badge/dependencies-none-brightgreen.svg)\n![](https://img.shields.io/npm/dt/funthreads.svg)\n![](https://img.shields.io/npm/l/funthreads.svg)\n\n# funthreads\nA simple library that provides an abstraction for the Node.js `worker_threads` module.\nIt enables you to run your function in a separate thread. You receive a Promise that resolves with the result of your function.\n\n### Example\n```js\nconst { executeInThread } = require('funthreads');\n\n// heavy operation (this will not block the main thread)\nconst num = await executeInThread((limit) =\u003e {\n  let result = 0, i = 1;\n\n  while (i \u003c= limit) {\n    result += i.toString().split('').reverse().join('').length;\n    i++;\n  }\n\n  return result;\n}, 12345678);\n```\n\nThis example highlights the optimization of a resource-intensive calculation. By executing the function in a separate thread, we prevent the main thread from being blocked.\n\n**Surprisingly simple, isn't it?**\n\n## Installation\n\n```shell\n$ npm i funthreads\n```\n\n## All examples:\n- [Basic example](https://github.com/nairihar/funthreads/tree/master/examples/basic.js)\n- [Parameters for the thread task](https://github.com/nairihar/funthreads/blob/master/examples/multi-params.js)\n- [Async function inside the thread](https://github.com/nairihar/funthreads/blob/master/examples/async-task.js)\n- [Error handling](https://github.com/nairihar/funthreads/blob/master/examples/error-handling.js)\n- [Use modules inside the thread](https://github.com/nairihar/funthreads/blob/master/examples/modules-in-thread.js)\n\n## Contributing\n\nSee the [contributing guide](https://github.com/nairihar/funthreads/blob/master/CONTRIBUTING.md) for detailed instructions on how to get started with our project.\n\n## API\n\n### `executeInThread(task, ...params)`\nRuns the specified function in a separate thread.\n\n#### Parameters\n- `Task (Function)`: The function to be executed in a thread.\n    - This can also be a async function (promise).\n- `...params (Any)`: Additional arguments to be passed to the Task function.\n    - Parameter cann't be a function.\n\n```js\nconst task = function() { ... };\nexecuteInThread(task, 'John', true, {}, ...);\n```\n\nThe `executeInThread` function allows you to execute a given task function in a dedicated thread, similar to the behavior of `setTimeout` or `setInterval`. You provide the main function to be executed, along with any additional arguments (...args) that should be passed to the given function.\n\n#### Returns\n`Promise\u003cany\u003e`: A Promise that resolves with the return value of the callback.\n\nInside the provided function, you have the flexibility to return any value, including a Promise. The returned value, whether it's a standard value or a Promise, will be passed back to you as the resolved result of the `Promise` returned by the `executeInThread` function.\n\n```js\nconst number = await executeInThread(() =\u003e 123); // 123\nconst name = await executeInThread(() =\u003e Promise.resolve('John')); // John\n```\n\n#### Important (limitation)\n\nAccess to data outside of the task function is restricted. If you require the use of a module, it should be required within the task function. The sole method for accessing data within a task function from external sources is through the utilization of the parameters. Closures do not function in this context.\n\nIn this example, we're reading a file in a separate thread and returning the data in string format. We start by defining a task function that will run within the thread, and then we prepare the necessary parameters to be passed as inputs to that function.\n\n```javascript\nconst { executeInThread } = require('funthreads');\n\nasync function task(fileName) {\n// Closure doesn't work here\n  const { readFile } = require('fs/promises');\n  const content = await readFile(__filename);\n  return content.toString();\n}\n\nasync function read() {\n  const content = await executeInThread(task, fileName);\n  console.log(content);\n}\n\nread();\n```\n\nThere is also another option if you don't want to use `require` inside the function.\n\n```js\nconst { executeInThread, ThreadModules } = require('funthreads');\n\nasync function task(modules) {\n  // Closure doesn't work here\n  const { readFile } = modules['fs/promises'];\n  const content = await readFile(__filename);\n  return content.toString();\n}\n\nasync function read() {\n  const requiredModules = new ThreadModules('fs/promises', 'test', 'path', ...);\n  const content = await executeInThread(task, requiredModules);\n  console.log(content);\n}\n\nread();\n```\n\nThe `ThreadModules` class lets you set up modules for the thread. You can provide it only thorough the second argument, and you'll have access to the libraries through the `modules` object.\n\nYou should only provide the `ThreadModules` type of object once through the second parameter. Attempting to provide it multiple times will result in an error. Additionally, avoid returning the `modules` object from the task function, as it will also lead to errors.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnairihar%2Ffunthreads","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnairihar%2Ffunthreads","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnairihar%2Ffunthreads/lists"}