New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

@moznion/limited-concurrent-workers

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@moznion/limited-concurrent-workers

A library that provides a limited number of concurrent workers

latest
Source
npmnpm
Version
0.0.1
Version published
Weekly downloads
3
Maintainers
1
Weekly downloads
 
Created
Source

limited-concurrent-workers-js .github/workflows/check.yml codecov npm version

A library that provides a limited number of concurrent workers for JavaScript/Typescript.

Motivation

In JavaScript, if it needs to run the async functions parallelly with a restriction of the number of concurrent workers, the code has to be a little idiomatic. This library provides some wrappers for that purpose by using the Generator, for easiness and simplicity.

Synopsis

function* generator(): IterableIterator<string> {
  for (let i = 0; i < 100; i++) {
    yield i.toString();
  }
}

// do something by three workers for the [0..99] numbers that come from the `generator()`.
const result = await runConcurrent<string, number>(
  3,
  arg => (resolve, reject) => {
    // do something
    resolve(Number(arg));
  },
  generator,
);

// example of the `result`
//    result[0]: [
//       0,  3,  6,  9, 12, 15, 18, 21, 24,
//      27, 30, 33, 36, 39, 42, 45, 48, 51,
//      54, 57, 60, 63, 66, 69, 72, 75, 78,
//      81, 84, 87, 90, 93, 96, 99
//    ]
//
//    result[1]: [
//       1,  4,  7, 10, 13, 16, 19, 22, 25,
//      28, 31, 34, 37, 40, 43, 46, 49, 52,
//      55, 58, 61, 64, 67, 70, 73, 76, 79,
//      82, 85, 88, 91, 94, 97
//    ]
//
//    result[2]: [
//       2,  5,  8, 11, 14, 17, 20, 23, 26,
//      29, 32, 35, 38, 41, 44, 47, 50, 53,
//      56, 59, 62, 65, 68, 71, 74, 77, 80,
//      83, 86, 89, 92, 95, 98
//    ]

Supported Functions

  • async function runConcurrent<T, R>(maxConcurrency: number, executorProvider: (arg: T) => (resolve: (value: R | PromiseLike<R>) => void, reject: (reason?: unknown) => void) => void, generatorFunction: () => IterableIterator<T>): Promise<Awaited<R[][]>>
  • async function runConcurrentFlatten<T, R>(maxConcurrency: number, executorProvider: (arg: T) => (resolve: (value: R | PromiseLike<R>) => void, reject: (reason?: unknown) => void) => void, generatorFunction: () => IterableIterator<T>): Promise<Awaited<R[]>>
  • async function runConcurrentSettled<T, R>(maxConcurrency: number, executorProvider: (arg: T) => (resolve: (value: R | PromiseLike<R>) => void, reject: (reason?: unknown) => void) => void, generatorFunction: () => IterableIterator<T>): Promise<PromiseSettledResult<R[]>[]>

Please see also typedoc.

Author

moznion (moznion@mail.moznion.net)

FAQs

Package last updated on 05 Dec 2022

Did you know?

Socket

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.

Install

Related posts