tiny-async-pool

WebJar for tiny-async-pool

Лицензия

Лицензия

MIT
Группа

Группа

org.webjars.npm
Идентификатор

Идентификатор

tiny-async-pool
Последняя версия

Последняя версия

1.0.4
Дата

Дата

Тип

Тип

jar
Описание

Описание

tiny-async-pool
WebJar for tiny-async-pool
Ссылка на сайт

Ссылка на сайт

http://webjars.org
Система контроля версий

Система контроля версий

https://github.com/rxaviers/async-pool

Скачать tiny-async-pool

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/org.webjars.npm/tiny-async-pool/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>tiny-async-pool</artifactId>
    <version>1.0.4</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/tiny-async-pool/
implementation 'org.webjars.npm:tiny-async-pool:1.0.4'
// https://jarcasting.com/artifacts/org.webjars.npm/tiny-async-pool/
implementation ("org.webjars.npm:tiny-async-pool:1.0.4")
'org.webjars.npm:tiny-async-pool:jar:1.0.4'
<dependency org="org.webjars.npm" name="tiny-async-pool" rev="1.0.4">
  <artifact name="tiny-async-pool" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='tiny-async-pool', version='1.0.4')
)
libraryDependencies += "org.webjars.npm" % "tiny-async-pool" % "1.0.4"
[org.webjars.npm/tiny-async-pool "1.0.4"]

Зависимости

compile (2)

Идентификатор библиотеки Тип Версия
org.webjars.npm : yaassertion jar [1.0.0,2)
org.webjars.npm : semver jar [5.5.0,6)

Модули Проекта

Данный проект не имеет модулей.

asyncPool

Why?

Existing solutions also re-implement Promise 😩 ...

The goal of this library is to use native async functions (if ES7 is available) and/or native Promise (ES6) including Promise.race() and Promise.all() to implement the concurrency behavior (look our source code).

What?

asyncPool runs multiple promise-returning & async functions in a limited concurrency pool. It rejects immediately as soon as one of the promises rejects. It resolves when all the promises completes. It calls the iterator function as soon as possible (under concurrency limit). For example:

const timeout = i => new Promise(resolve => setTimeout(() => resolve(i), i));
await asyncPool(2, [1000, 5000, 3000, 2000], timeout);
// Call iterator (i = 1000)
// Call iterator (i = 5000)
// Pool limit of 2 reached, wait for the quicker one to complete...
// 1000 finishes
// Call iterator (i = 3000)
// Pool limit of 2 reached, wait for the quicker one to complete...
// 3000 finishes
// Call iterator (i = 2000)
// Itaration is complete, wait until running ones complete...
// 5000 finishes
// 2000 finishes
// Resolves, results are passed in given array order `[1000, 5000, 3000, 2000]`.

Usage

$ npm install tiny-async-pool
import asyncPool from "tiny-async-pool";

ES7 async

const timeout = i => new Promise(resolve => setTimeout(() => resolve(i), i));
const results = await asyncPool(2, [1000, 5000, 3000, 2000], timeout);

Note: Something really nice will be possible soon https://github.com/tc39/proposal-async-iteration

ES6 Promise

const timeout = i => new Promise(resolve => setTimeout(() => resolve(i), i));
return asyncPool(2, [1000, 5000, 3000, 2000], timeout).then(results => {
  ...
});

API

asyncPool(poolLimit, array, iteratorFn)

Runs multiple promise-returning & async functions in a limited concurrency pool. It rejects immediately as soon as one of the promises rejects. It resolves when all the promises completes. It calls the iterator function as soon as possible (under concurrency limit).

poolLimit

The pool limit number (>= 1).

array

Input array.

iteratorFn

Iterator function that takes two arguments (array item and the array itself). The iterator function should either return a promise or be an async function.

License

MIT © Rafael Xavier de Souza

Версии библиотеки

Версия
1.0.4