Product
Introducing Enhanced Alert Actions and Triage Functionality
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
@supercharge/promise-pool
Advanced tools
Changelog
2.2.0 - 2022-05-20
pool.onTaskStarted((item, pool) => { … })
: configure a callback that runs when an item is about to be processedpool.onTaskFinished((item, pool) => { … })
: configure a callback that runs when an item finished processingReadme
Map-like, concurrent promise processing for Node.js.
Installation · Docs · Usage
Follow @marcuspoehls and @superchargejs for updates!
npm i @supercharge/promise-pool
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:
const { PromisePool } = require('@supercharge/promise-pool')
const users = [
{ name: 'Marcus' },
{ name: 'Norman' },
{ name: 'Christian' }
]
const { results, errors } = await PromisePool
.withConcurrency(2)
.for(users)
.process(async (userData, index, pool) => {
const user = await User.createIfNotExisting(userData)
return user
})
The promise pool uses a default concurrency of 10:
await PromisePool
.for(users)
.process(async data => {
// processes 10 items in parallel by default
})
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:
await PromisePool
.for(users)
.process(async (user, index, pool) => {
if (condition) {
return pool.stop()
}
// processes the `user` data
})
You may also stop the pool from within the .handleError()
method in case you need to:
const { PromisePool } = require('@supercharge/promise-pool')
await PromisePool
.for(users)
.handleError(async (error, user, pool) => {
if (error instanceof SomethingBadHappenedError) {
return pool.stop()
}
// handle the given `error`
})
.process(async (user, index, pool) => {
// processes the `user` data
})
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.
const { PromisePool } = require('@supercharge/promise-pool')
try {
const errors = []
const { results } = await PromisePool
.for(users)
.withConcurrency(4)
.handleError(async (error, user) => {
if (error instanceof ValidationError) {
errors.push(error) // you must collect errors yourself
return
}
if (error instanceof ThrottleError) { // Execute error handling on specific errors
await retryUser(user)
return
}
throw error // Uncaught errors will immediately stop PromisePool
})
.process(async data => {
// the harder you work for something,
// the greater you’ll feel when you achieve it
})
await handleCollected(errors) // this may throw
return { results }
} catch (error) {
await handleThrown(error)
}
You can receive a callback when any task has started with .onTaskStarted()
with the item
has starting this process, with percentage
of progress the items that has started, activeTasks
that is processing and finishedTasks
:
const { PromisePool } = require('@supercharge/promise-pool')
await PromisePool
.for(users)
.onTaskStarted((item, percentage, activeTasks, finishedTasks) => {
console.log(`Progress: ${percentage}%`)
console.log(`Active tasks: ${activeTasks.length}`)
console.log(`Finished tasks: ${finishedTasks.length}`)
})
.process(async (user, index, pool) => {
// processes the `user` data
})
You can also receive callback .onTaskFinished()
, with parameter percentage
of progress the items that has finished:
const { PromisePool } = require('@supercharge/promise-pool')
await PromisePool
.for(users)
.onTaskFinished((item, percentage, activeTasks, finishedTasks) => {
console.log(`Progress: ${percentage}%`)
console.log(`Active tasks: ${activeTasks.length}`)
console.log(`Finished tasks: ${finishedTasks.length}`)
})
.process(async (user, index, pool) => {
// processes the `user` data
})
git checkout -b my-feature
git commit -am 'Add some feature'
git push origin my-new-feature
MIT © Supercharge
superchargejs.com · GitHub @supercharge · Twitter @superchargejs
FAQs
Map-like, concurrent promise processing for Node.js
The npm package @supercharge/promise-pool receives a total of 311,327 weekly downloads. As such, @supercharge/promise-pool popularity was classified as popular.
We found that @supercharge/promise-pool demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.