Installations
npm install @kayanski/promise-pool
Developer Guide
Typescript
Yes
Module System
CommonJS
Min. Node Version
>=8
Node Version
16.14.2
NPM Version
8.5.0
Score
74
Supply Chain
99.4
Quality
75.3
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (52.93%)
TypeScript (47.07%)
Developer
Download Statistics
Total Downloads
692
Last Day
2
Last Week
4
Last Month
14
Last Year
213
GitHub Statistics
794 Stars
332 Commits
41 Forks
11 Watching
1 Branches
15 Contributors
Package Meta Information
Latest Version
3.0.3
Package Id
@kayanski/promise-pool@3.0.3
Unpacked Size
42.32 kB
Size
8.89 kB
File Count
19
NPM Version
8.5.0
Node Version
16.14.2
Total Downloads
Cumulative downloads
Total Downloads
692
Last day
0%
2
Compared to previous day
Last week
300%
4
Compared to previous week
Last month
100%
14
Compared to previous month
Last year
-3.2%
213
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Promise Pool
Map-like, concurrent promise processing for Node.js.
Installation · Docs · Usage
Follow @marcuspoehls and @superchargejs for updates!
Installation
npm i @supercharge/promise-pool
Docs
Usage
Using the promise pool is pretty straightforward. The package exposes a class and you can create a promise pool instance using the fluent interface.
Here’s an example using a concurrency of 2:
1const { PromisePool } = require('@supercharge/promise-pool') 2 3const users = [ 4 { name: 'Marcus' }, 5 { name: 'Norman' }, 6 { name: 'Christian' } 7] 8 9const { results, errors } = await PromisePool 10 .withConcurrency(2) 11 .for(users) 12 .process(async (userData, index, pool) => { 13 const user = await User.createIfNotExisting(userData) 14 15 return user 16 })
The promise pool uses a default concurrency of 10:
1await PromisePool 2 .for(users) 3 .process(async data => { 4 // processes 10 items in parallel by default 5 })
Manually Stop the Pool
You can stop the processing of a promise pool using the pool
instance provided to the .process()
and .handleError()
methods. Here’s an example how you can stop an active promise pool from within the .process()
method:
1await PromisePool 2 .for(users) 3 .process(async (user, index, pool) => { 4 if (condition) { 5 return pool.stop() 6 } 7 8 // processes the `user` data 9 })
You may also stop the pool from within the .handleError()
method in case you need to:
1const { PromisePool } = require('@supercharge/promise-pool') 2 3await PromisePool 4 .for(users) 5 .handleError(async (error, user, pool) => { 6 if (error instanceof SomethingBadHappenedError) { 7 return pool.stop() 8 } 9 10 // handle the given `error` 11 }) 12 .process(async (user, index, pool) => { 13 // processes the `user` data 14 })
Bring Your Own Error Handling
The promise pool allows for custom error handling. You can take over the error handling by implementing an error handler using the .handleError(handler)
.
If you provide an error handler, the promise pool doesn’t collect any errors. You must then collect errors yourself.
Providing a custom error handler allows you to exit the promise pool early by throwing inside the error handler function. Throwing errors is in line with Node.js error handling using async/await.
1const { PromisePool } = require('@supercharge/promise-pool') 2 3try { 4 const errors = [] 5 6 const { results } = await PromisePool 7 .for(users) 8 .withConcurrency(4) 9 .handleError(async (error, user) => { 10 if (error instanceof ValidationError) { 11 errors.push(error) // you must collect errors yourself 12 return 13 } 14 15 if (error instanceof ThrottleError) { // Execute error handling on specific errors 16 await retryUser(user) 17 return 18 } 19 20 throw error // Uncaught errors will immediately stop PromisePool 21 }) 22 .process(async data => { 23 // the harder you work for something, 24 // the greater you’ll feel when you achieve it 25 }) 26 27 await handleCollected(errors) // this may throw 28 29 return { results } 30} catch (error) { 31 await handleThrown(error) 32}
Callback for Started and Finished Tasks
You can use the onTaskStarted
and onTaskFinished
methods to hook into the processing of tasks. The provided callback for each method will be called when a task started/finished processing:
1const { PromisePool } = require('@supercharge/promise-pool') 2 3await PromisePool 4 .for(users) 5 .onTaskStarted((item, pool) => { 6 console.log(`Progress: ${pool.processedPercentage()}%`) 7 console.log(`Active tasks: ${pool.processedItems().length}`) 8 console.log(`Active tasks: ${pool.activeTasksCount()}`) 9 console.log(`Finished tasks: ${pool.processedItems().length}`) 10 console.log(`Finished tasks: ${pool.processedCount()}`) 11 }) 12 .onTaskFinished((item, pool) => { 13 // update a progress bar or something else :) 14 }) 15 .process(async (user, index, pool) => { 16 // processes the `user` data 17 })
You can also chain multiple onTaskStarted
and onTaskFinished
handling (in case you want to separate some functionality):
1const { PromisePool } = require('@supercharge/promise-pool') 2 3await PromisePool 4 .for(users) 5 .onTaskStarted(() => {}) 6 .onTaskStarted(() => {}) 7 .onTaskFinished(() => {}) 8 .onTaskFinished(() => {}) 9 .process(async (user, index, pool) => { 10 // processes the `user` data 11 })
Contributing
- Create a fork
- Create your feature branch:
git checkout -b my-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request 🚀
License
MIT © Supercharge
superchargejs.com · GitHub @supercharge · Twitter @superchargejs
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 existing vulnerabilities detected
Reason
Found 2/28 approved changesets -- score normalized to 0
Reason
0 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/run-tests.yml:21: update your workflow using https://app.stepsecurity.io/secureworkflow/supercharge/promise-pool/run-tests.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/run-tests.yml:24: update your workflow using https://app.stepsecurity.io/secureworkflow/supercharge/promise-pool/run-tests.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/run-tests.yml:30
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/run-tests.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 4 are checked with a SAST tool
Score
3.4
/10
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More