{"id":17595005,"url":"https://github.com/djwassink/promise-parallel-throttle","last_synced_at":"2025-04-12T20:44:01.356Z","repository":{"id":86418190,"uuid":"71500828","full_name":"DJWassink/Promise-parallel-throttle","owner":"DJWassink","description":"It's kinda like Promise.all(), but throttled!","archived":false,"fork":false,"pushed_at":"2024-12-03T09:33:47.000Z","size":319,"stargazers_count":81,"open_issues_count":2,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-04T00:08:42.826Z","etag":null,"topics":["parallel","promise","sequential","throttle"],"latest_commit_sha":null,"homepage":"","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/DJWassink.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}},"created_at":"2016-10-20T20:16:32.000Z","updated_at":"2024-07-13T01:36:11.000Z","dependencies_parsed_at":"2024-02-16T10:45:31.789Z","dependency_job_id":"219ea9b5-95b3-48de-9b65-be0506148614","html_url":"https://github.com/DJWassink/Promise-parallel-throttle","commit_stats":{"total_commits":50,"total_committers":5,"mean_commits":10.0,"dds":0.26,"last_synced_commit":"01f9ae23904483a7459e94a08721425534a8decc"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DJWassink%2FPromise-parallel-throttle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DJWassink%2FPromise-parallel-throttle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DJWassink%2FPromise-parallel-throttle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DJWassink%2FPromise-parallel-throttle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DJWassink","download_url":"https://codeload.github.com/DJWassink/Promise-parallel-throttle/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248631687,"owners_count":21136556,"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":["parallel","promise","sequential","throttle"],"created_at":"2024-10-22T07:25:51.441Z","updated_at":"2025-04-12T20:44:01.329Z","avatar_url":"https://github.com/DJWassink.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Promise-parallel-throttle\n\n[![Build Status](https://github.com/DJWassink/Promise-parallel-throttle/actions/workflows/main.yml/badge.svg)](https://travis-ci.org/DJWassink/Promise-parallel-throttle)\n[![npm version](https://badge.fury.io/js/promise-parallel-throttle.svg)](https://badge.fury.io/js/promise-parallel-throttle)\n[![npm downloads](https://img.shields.io/npm/dm/promise-parallel-throttle.svg)](https://www.npmjs.com/package/promise-parallel-throttle)\n[![Bundlephobia](https://badgen.net/bundlephobia/min/promise-parallel-throttle)](https://bundlephobia.com/result?p=promise-parallel-throttle)\n[![Bundlephobia](https://badgen.net/bundlephobia/minzip/promise-parallel-throttle)](https://bundlephobia.com/result?p=promise-parallel-throttle)\n\nRun a array of Promises in parallel. Kinda like Promise.all(), but throttled!\n\n## Install\n\n### NPM\n\n```bash\nnpm i promise-parallel-throttle -S\n```\n\n### Yarn\n\n```bash\nyarn add promise-parallel-throttle\n```\n\n## Usage\n\n```js\nimport * as Throttle from 'promise-parallel-throttle';\n\n//Function which should return a Promise\nconst doReq = async (firstName, lastName) =\u003e {\n    //Do something async.\n    return firstName + ' ' + lastName;\n};\n\nconst users = [\n    {firstName: 'Irene', lastName: 'Pullman'},\n    {firstName: 'Sean', lastName: 'Parr'},\n];\n\n//Queue with functions to be run\nconst queue = users.map((user) =\u003e () =\u003e doReq(user.firstName, user.lastName));\n\n//Default Throttle runs with 5 promises parallel.\nconst formattedNames = await Throttle.all(queue);\n\nconsole.log(formattedNames); //['Irene Pullman', 'Sean Parr']\n```\n\n[![Edit Promise-parallel-throttle example](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/4x1943m2v7)\n\n## API\n\n### Throttle.all\n\n`Throttle.all(tasks, options)`\n\nThrottle.all is made to behave exactly like Promise.all but instead of all the tasks running in parallel it runs a maxium amount of tasks in parallel.\nOnly the tasks parameter is required while the [options](#options-object) parameter is optional.\n\n### Throttle.sync\n\n`Throttle.sync(tasks, options)`\n\nThrottle.sync runs all the tasks synchronously.\nOnce again the tasks array is required, the [options](#options-object) are optional.\nBe aware that this method is simply a wrapper to pass `maxInProgress` with 1. So overwriting this option in the options object would run the tasks again in parallel.\n\n### Throttle.raw\n\n`Throttle.raw(tasks, options)`\n\nThe raw method instead of returning the tasks their results, will return a [result](#result-object--progress-callback) object.\nUseful if you wan't more statistics about the execution of your tasks. Once again the tasks are required while the [options](#options-object) are optional.\n\n#### Option's Object\n\n| Parameter             | Type     | Default                                         | Definition                                                                                                                                                   |\n| :-------------------- | :------- | :---------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| maxInProgress         | Integer  | 5                                               | max amount of parallel threads                                                                                                                               |\n| failFast              | Boolean  | true (false for the [raw](#throttleraw) method) | reject after a single error, or keep running                                                                                                                 |\n| progressCallback      | Function | Optional                                        | callback with progress reports                                                                                                                               |\n| nextCheck             | Function | Optional                                        | function which should return a promise, if the promise resolved true the next task is spawn, errors will propagate and should be handled in the calling code |\n| ignoreIsFunctionCheck | Boolean  | false                                           | If one of the tasks is not a function an error is thrown, if this boolean is set to true we simply return the task itself                                    |\n\n#### Result object / Progress callback\n\nThe `progressCallback` and the `Raw` will return a `Result` object with the following properties:\n\n| Property               | Type    | Start value | Definition                                                                                   |\n| :--------------------- | :------ | :---------- | :------------------------------------------------------------------------------------------- |\n| lastCompletedIndex     | Integer | -1          | last index of a task that is completed (either fulfilled or rejected)                        |\n| amountDone             | Integer | 0           | amount of tasks which are finished                                                           |\n| amountStarted          | Integer | 0           | amount of tasks which started                                                                |\n| amountResolved         | Integer | 0           | amount of tasks which successfully resolved                                                  |\n| amountRejected         | Integer | 0           | amount of tasks which returned in an error and are aborted                                   |\n| amountNextCheckFalsey  | Integer | 0           | amount of tasks which got a falsey value in the [nextCheck](#nextcheck)                      |\n| rejectedIndexes        | Array   | []          | all the indexes in the tasks array where the promise rejected                                |\n| resolvedIndexes        | Array   | []          | all the indexes in the tasks array where the promise resolved                                |\n| nextCheckFalseyIndexes | Array   | []          | all the indexes in the tasks array where the [nextCheck](#nextcheck) returned a falsey value |\n| taskResults            | Array   | []          | array containing the result of every task                                                    |\n\n#### nextCheck\n\nAll the `Throttle` methods have a `nextCheck` method which will be used to verify if a next task is allowed to start.\n\nThe default `nextCheck` is defined like this;\n\n```js\nconst defaultNextTaskCheck = (status, tasks) =\u003e {\n    return new Promise((resolve, reject) =\u003e {\n        resolve(status.amountStarted \u003c tasks.length);\n    });\n};\n```\n\nThis function will get a status object as parameter which adheres to the [Result object](#result-object--progress-callback) and it also receives the list of tasks.\nIn the default `nextCheck` we simply check if the amount of started exceeds the amount to be done, if not we are free to start an other task.\n\nThis function can be useful to write your own scheduler based on, for example ram/cpu usage.\nLets say that your tasks use a lot of ram and you don't want to exceed a certain amount.\nYou could then write logic inside a `nextCheck` function which resolves after there is enough ram available to start the next task.\n\nIf a custom implementation decides to reject, the error is propagated and should be handled in the user it's code. If a custom implementation returns a falsey value the task will simply not execute and the next task will be scheduled.\n\n## Example\n\nCheck out the example's directory, it's heavily documented so it should be easy to follow.\n\nTo run the example, at least Node 8.x.x is required, since it supports native async/await.\n\nSimply run the example with npm:\n\n```\nnpm run-script names\n```\n\nOr with Yarn:\n\n```\nyarn names\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdjwassink%2Fpromise-parallel-throttle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdjwassink%2Fpromise-parallel-throttle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdjwassink%2Fpromise-parallel-throttle/lists"}