
Research
Two Malicious Rust Crates Impersonate Popular Logger to Steal Wallet Keys
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
eager-async-pool
Advanced tools
Simple to use, fully asynchronous and iterable-based async pool for JavaScript and TypeScript. Works with native Promises.
Promise
and iteratorsnpm install eager-async-pool
or yarn add eager-async-pool
const { asyncPool } = require('eager-async-pool')
// exceptionally useful executor
const executor = (n) => 'Number is: ' + n
// absolutely realistic dataset
const items = [1, 2, 3, 4]
for await (const { value, error } of asyncPool(executor, items, { limit: 2 })) {
if (error) {
console.error(error)
} else {
console.log(value)
}
}
First, write an executor function, which will do some kind of task:
const executor = (url) => fetch(url).then(i => i.text())
Then, create an array (or any iterable) that contains the data that will be passed to the executor one-by-one:
const urls = ['https://google.com', 'https://yandex.ru', 'https://bing.com']
Finally, call the asyncPool
and handle the results!
for await (const { idx, item, value, error } of asyncPool(executor, urls)) {
console.log(`${item} title: ${cheerio.load(value).find('title').text()}`)
}
Alternatively, you can use asyncPoolCallback
that wraps over asyncPool
and allows
you to specify a callback rather than using for-await
loop:
await asyncPoolCallback(executor, urls, ({ idx, item, value, error }) => {
console.log(`${item} title: ${cheerio.load(value).find('title').text()}`)
})
Of course, while processing items you may very well encounter some kind of error.
Luckily, it is very easy to handle them with eager-async-pool
!
When executor
throws an error, iterator will yield an object containing .error
,
which will contain that very error. To retry the failed item, just push it to the array!
for await (const { idx, item, value, error } of asyncPool(executor, urls)) {
if (error) {
urls.push(item)
continue
}
// ...rest of the logic...
}
eager-async-pool
also supports cancel tokens. They are extremely easy to use as well.
Just use CancelToken.source()
, pass the token to asyncPool
and .cancel()
when you feel like:
const cancel = CancelToken.source()
for await (const { idx, item, value, error } of asyncPool(executor, urls, { cancel: cancel.token })) {
if (error) {
cancel.cancel('An error has occurred')
break
}
console.log(`${item} title: ${cheerio.load(value).find('title').text()}`)
}
Note:
break
in an iterableasyncPool
will also effectively cancel the pool, butbreak
is (for obvious reasons) not available inasyncPoolCallback
.Also, cancel token is compatible with other libraries that support it, so you can use it inside executors as well.
Note: cancelling an async pool only guarantees that executor will not be called anymore. It DOES NOT cancel pending operations, nor does it ignore them.
If you want to cancel pending operations as well, you must handle cancel token inside executor manually (or provide it to some library).
Consider the common task of batch file upload/download.
You could try to do that using built-in Promise.all()
, but that's probably not
the best idea, since it will probably slow down the process and use a lot of resources,
and server might be limiting concurrent connections.
Also, when downloading you might encounter an error (e.g. rate limit), and you won't want that error to interrupt the entire download process, and instead you'd want to retry downloading that particular file.
You may also want to do something immediately after the file is downloaded, and not wait until all other files are downloaded as well.
FAQs
Fully asynchronous iterable-based async pool for JS
The npm package eager-async-pool receives a total of 90 weekly downloads. As such, eager-async-pool popularity was classified as not popular.
We found that eager-async-pool demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
Research
A malicious package uses a QR code as steganography in an innovative technique.
Research
/Security News
Socket identified 80 fake candidates targeting engineering roles, including suspected North Korean operators, exposing the new reality of hiring as a security function.