Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
async-parallel
Advanced tools
Async enabled each(), map(), filter() functions that work just like their standard counterparts, but can be used with async/await and also provide concurrency limiting. Includes built-in typings and JSDoc comments for IntelliSense documentation.
Async enabled each(), map(), filter() functions that work just like their standard counterparts, but can be used with async/await and also provide concurrency limiting. Includes built-in typings and JSDoc comments for IntelliSense documentation.
The following iterative functions are provided:
Every function above provides a concurrency
parameter to limit the maximum number of parallel instances at the function call level. In addition, concurrency can be limited at a global level with the following function:
concurrency
parameter at the function-call level.The following additional utility functions are also provided:
var list = [100, 200, 300]; // provide list of inputs here
await Parallel.each(list, async item => {
// process each item here
});
var list = [100, 200, 300]; // provide list of inputs here
var result = await Parallel.map(list, async item => {
// process each item here
});
// result available here
var list = [100, 200, 300]; // provide list of inputs here
var result = await Parallel.filter(list, async item => {
// test each item here returning true to include or false to reject
});
// result available here
await Parallel.invoke([
async () => { /* task #1 here */ },
async () => { /* task #2 here */ },
async () => { /* task #3 here */ },
async () => { /* task #4 here */ },
async () => { /* task #5 here */ }
], 2);
Note: The same result can be achieved without a library using
Promise.all
, howeverParallel.invoke
provides an ability to limit the concurrency. Therefore, in example above only 2 of the tasks will be run at the same time.
Make sure you're running Node v4 or higher and TypeScript 1.8 or higher...
$ node -v
v7.3.3
$ npm install -g typescript
$ tsc -v
Version 2.3.4
Install package...
$ npm install async-parallel
Write some code...
import * as Parallel from 'async-parallel';
(async function () {
var list = [100, 200, 300];
var start = new Date();
await Parallel.each(list, async value => {
await Parallel.sleep(value);
console.log('sleep', value);
});
console.log('done', new Date().getTime() - start.getTime());
})();
Save the above to a file index.ts
, build and run it!
$ tsc index.ts --target es6 --module commonjs
$ node index.js
sleep 100
sleep 200
sleep 300
done 303
The number of concurrent actions can be limited at the function level, or by calling the Parallel.setConcurrency()
which sets a default concurrency setting.
Examples:
await Parallel.each([100, 200, 300], async item => {
// process each item here
}, 2); // process no more than 2 items at the same time
await Parallel.invoke([
async () => { /* task #1 here */ },
async () => { /* task #2 here */ },
async () => { /* task #3 here */ },
async () => { /* task #4 here */ },
async () => { /* task #5 here */ }
], 3); // process no more than 3 items at the same time
Parallel.setConcurrency(10); // no more than 10 actions running at the same time by default
If one or more actions fail then no further actions will be started and a rollup error will result after all pending actions are complete. The rollup error will contain a list of individual failures as shown below.
try {
await Parallel.pool(2, async () =>
await someRecurringTask());
}
catch (err) {
console.log(err.message); // print the rollup error message
for (var item of err.list)
console.log(item.message); // print each specific error message
}
Create several actions, running no more than 2 at a time.
var actions = [
async function () { /* task #1 here */ },
async function () { /* task #2 here */ },
async function () { /* task #3 here */ },
async function () { /* task #4 here */ },
async function () { /* task #5 here */ }
];
await Parallel.pool(2, async () => {
var action = actions.shift();
await action();
return action.length > 0;
});
// all actions are complete here
Calls a provided function once per input in parallel.
each<T1, T2>(list: T1[], action: {(value: T1): Promise<T2>}, concurrency?: number): Promise<void>
Parameter | Type | Description |
---|---|---|
list | (generic) | A list of input elements to iterate. |
action | function | An async function callback invoked for each element in the list. The callback takes three arguments: the current element being processed, the index of the current element, and the input list. |
concurrency | number | Limits the number of callback actions to run concurrently. |
Tests whether all elements in the array pass the test implemented by the provided function.
every<T>(list: T[], action: {(value: T, index: number, list: T[]): Promise<boolean>}, concurrency?: number): Promise<boolean>
Parameter | Type | Description |
---|---|---|
list | (generic) | A list of input elements to test. |
action | function | An async function callback invoked for each element in the list. The callback takes three arguments: the current element being processed, the index of the current element, and the input list. The callback resolves to true for elements that pass the test. |
concurrency | number | Limits the number of callback actions to run concurrently. |
Returns true if every test resolved to true, otherwise false.
Creates a new array with all elements that pass the test implemented by the provided function in parallel.
filter<T>(list: T[], action: {(value: T, index: number, list: T[]): Promise<boolean>}, concurrency?: number): Promise<T[]>
Parameter | Type | Description |
---|---|---|
list | (generic) | A list of input elements to test. |
action | function | An async function callback invoked for each element in the list. The callback takes three arguments: the current element being processed, the index of the current element, and the input list. The callback resolves to true for elements to be included in the output list. |
concurrency | number | Limits the number of callback actions to run concurrently. |
Returns a list of filtered elements in the same order as the input.
Calls a set of provided functions in parallel.
invoke(list: {(): Promise<void>}[], concurrency?: number): Promise<void>
Parameter | Type | Description |
---|---|---|
list | function[] | A list of async function callbacks to invoke. The callback takes no arguments and resolves to a void. |
concurrency | number | Limits the number of callback actions to run concurrently. |
Creates a new array with the results of calling a provided function in parallel on every input. The output will be in the same order as the input.
map<T1, T2>(list: T1[], action: {(value: T1, index: number, list: T1[]): Promise<T2>}, concurrency?: number): Promise<T2[]>
Parameter | Type | Description |
---|---|---|
list | (generic) | A list of input elements to map. |
action | function | An async function callback that produces an element of the output list. The callback takes three arguments: the current element being processed, the index of the current element, and the input list. The callback resolves to a single output element. |
concurrency | number | Limits the number of callback actions to run concurrently. |
Returns a list of mapped elements in the same order as the input.
Repeatedly invokes a provided async function that resolves to a boolean result. A pool of parallel instances is maintained until a true result is obtained, after which no new instances will be invoked.
pool(size: number, task: {(): Promise<boolean>}): Promise<void>
Parameter | Type | Description |
---|---|---|
size | number | Specifies the size of the pool indicating the number of parallel instances of the provided async function to maintain. |
task | function | The provided async function callback that takes no arguments and resolves to a boolean. Returning true indicates no new instances should be invoked. |
Applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.
reduce<T1, T2>(list: T1[], action: {(accumulator: T2, value: T1, index: number, list: T1[]): Promise<T2>}, value: T2, concurrency?: number): Promise<T2>
Parameter | Type | Description |
---|---|---|
list | (generic) | A list of input elements to reduce. |
action | function | An async function callback invoked for each element in the list. The callback takes four arguments: the accumulated value previously returned in the last invocation of the callback or initialValue, the current element being processed, the index of the current element, and the input list. The callback resolves to an updated accumulated value. |
initialValue | (generic) | Value to use as the first argument to the first call of the callback. |
concurrency | number | Limits the number of callback actions to run concurrently. |
Returns the value that results from the reduction.
Sets a default that limits the number of concurrent callback actions for all parallel functions. Specifying the concurrency at the function level supercedes this setting.
setConcurrency(value: number): void
Parameter | Type | Description |
---|---|---|
value | number | Specifies the new default concurrency setting. |
Sleeps for the specified duration.
sleep(milliseconds: number): Promise<void>
Parameter | Type | Description |
---|---|---|
milliseconds | number | The amount of time to sleep in milliseconds. |
Tests whether some element in the array passes the test implemented by the provided function.
some<T>(list: T[], action: {(value: T, index: number, list: T[]): Promise<boolean>}, concurrency?: number): Promise<boolean>
Parameter | Type | Description |
---|---|---|
list | (generic) | A list of input elements to test. |
action | function | An async function callback invoked for each element in the list. The callback takes three arguments: the current element being processed, the index of the current element, and the input list. The callback resolves to true for elements that pass the test. |
concurrency | number | Limits the number of callback actions to run concurrently. |
Returns true if some (at least one) test resolved to true, otherwise false.
FAQs
Async enabled each(), map(), filter() functions that work just like their standard counterparts, but can be used with async/await and also provide concurrency limiting. Includes built-in typings and JSDoc comments for IntelliSense documentation.
The npm package async-parallel receives a total of 5,080 weekly downloads. As such, async-parallel popularity was classified as popular.
We found that async-parallel 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
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.