{"id":18985469,"url":"https://github.com/kritollm/concurrent-wrapper","last_synced_at":"2025-08-10T19:26:49.117Z","repository":{"id":57205214,"uuid":"86786834","full_name":"kritollm/concurrent-wrapper","owner":"kritollm","description":"Easily add logic to limit concurrent execution of any function returning a Promise.","archived":false,"fork":false,"pushed_at":"2017-04-02T21:27:18.000Z","size":17,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-02T07:15:30.183Z","etag":null,"topics":["async","concurrent","max","parallel","promise"],"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/kritollm.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":"2017-03-31T06:41:53.000Z","updated_at":"2017-04-05T23:45:53.000Z","dependencies_parsed_at":"2022-09-18T00:50:47.533Z","dependency_job_id":null,"html_url":"https://github.com/kritollm/concurrent-wrapper","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kritollm%2Fconcurrent-wrapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kritollm%2Fconcurrent-wrapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kritollm%2Fconcurrent-wrapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kritollm%2Fconcurrent-wrapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kritollm","download_url":"https://codeload.github.com/kritollm/concurrent-wrapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239994005,"owners_count":19730780,"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","concurrent","max","parallel","promise"],"created_at":"2024-11-08T16:26:34.853Z","updated_at":"2025-02-21T10:14:58.944Z","avatar_url":"https://github.com/kritollm.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# concurrent-wrapper\n\n## Description\nEasily add logic to limit concurrent execution of any function returning a Promise.\n\n## Reason\nIt is rarely a good thing to send off too many requests at once, so it's good to be able to limit it easy.\nSome api's have a limit on unhandled request.\n\n## Usage\n```bash\n$ npm install -S concurrent-wrapper\n```\n\n```javascript\n// var concurrentWrapper = require(concurrent-wrapper).concurrentWrapper;\nimport { concurrentWrapper } from 'concurrent-wrapper';\n\nlet withConcurrentLogic = concurrentWrapper(2, myAsyncFunction);\n\nlet params = [\"something\", \"somethingelse\", \"andanotherthing\", \"whattotype\"]\n\nparams.forEach(p =\u003e withConcurrentLogic(`https://unstable.com/api/findSomething?thing=${p}`)\n    .then(r =\u003e {\n        //..Do something with result\n    })\n    .catch(e =\u003e {\n        // Retry 4 times was not enough,\n        // do something with the error\n    }));\n\n```\n\n\u003eIf my function doesn't return a Promise, am I doomed to live a life in callback hell making spaghetti code?\n\nFear not, you can use [this](https://www.npmjs.com/package/cb-topromise-wrapper).\n\n## Example\n\n```javascript\nimport { concurrentWrapper } from 'concurrent-wrapper';\n\nlet maxConcurrent = 3;\n// To log\nlet parallelRequests = 0;\n\nfunction simulateRequest(req) {\n\n    return new Promise((resolve, reject) =\u003e {\n\n        var random = Math.floor(Math.random() * 20);\n        parallelRequests++;\n        // Log parallel requests\n        console.log('Requests in parallel', parallelRequests);\n\n        // ---------------------------\n        setTimeout(() =\u003e {\n            let requestInParallel = parallelRequests;\n            parallelRequests--;\n            if (random \u003c 7) {\n                return reject({ ok: false, req, requestInParallel });\n            }\n            return resolve({ ok: true, req, requestInParallel });\n        }, random);\n    });\n}\n\nlet concurrentWrapped = concurrentWrapper(maxConcurrent, simulateRequest);\nlet promises = [];\nfor (let i = 0, l = 100; i \u003c l; i++) {\n    promises.push(concurrentWrapped(i)\n    // Catch so Promise.all(promises) isn't rejected if\n    // retry fails.\n    .catch(e =\u003e e));\n}\nfunction doSomething(r) {\n    console.log(r);\n    return r;\n}\nPromise.all(promises)\n    .then(res =\u003e res\n        .map(r =\u003e doSomething(r)));\n```\n## Tips\n\nYou can use it with retry-wrapper to also add retry logic to your async function.\n\n```bash\n$ npm install -S concurrent-wrapper retry-wrapper\n```\n\n```javascript\n// var concurrentWrapper = require(concurrent-wrapper).concurrentWrapper;\nimport { concurrentWrapper } from 'concurrent-wrapper';\n// var retryWrapper = require(retry-wrapper).retryWrapper;\nimport { retryWrapper } from 'retry-wrapper';\n\n// Fastest, retries must wait in que.\nlet retryAndConcurrent = retryWrapper(5, concurrentWrapper(5, myRequestFunction));\n// Slower, retries doesn't wait in que.\n//let retryAndConcurrent = concurrentWrapper(5, retryWrapper(5, myRequestFunction));\nfor (let i = 0, l = 1000; i \u003c l; i++) {\n    retryAndConcurrent(i).then(console.log.bind(console)).catch(console.error.bind(console))\n  }\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkritollm%2Fconcurrent-wrapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkritollm%2Fconcurrent-wrapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkritollm%2Fconcurrent-wrapper/lists"}