This commit is contained in:
Tutur33
2023-11-24 22:35:41 +01:00
parent 3c0b507a93
commit 7644b2a0f7
45165 changed files with 4803356 additions and 3 deletions
+195
View File
@@ -0,0 +1,195 @@
declare namespace pAll {
interface Options {
/**
Number of concurrent pending promises. Minimum: `1`.
@default Infinity
*/
concurrency?: number;
}
type PromiseFactory<T> = () => PromiseLike<T>;
}
/**
Run promise-returning & async functions concurrently with optional limited concurrency.
@param tasks - Iterable with promise-returning/async functions.
@returns A `Promise` that is fulfilled when all promises returned from calling the functions in `tasks` are fulfilled, or rejects if any of the promises reject. The fulfilled value is an `Array` of the fulfilled values in `tasks` order.
@example
```
import pAll = require('p-all');
import got = require('got');
(async () => {
const actions = [
() => got('https://sindresorhus.com'),
() => got('https://ava.li'),
() => checkSomething(),
() => doSomethingElse()
];
console.log(await pAll(actions, {concurrency: 2}));
})();
```
*/
declare const pAll: {
<
Result1,
Result2,
Result3,
Result4,
Result5,
Result6,
Result7,
Result8,
Result9,
Result10
>(
tasks: [
pAll.PromiseFactory<Result1>,
pAll.PromiseFactory<Result2>,
pAll.PromiseFactory<Result3>,
pAll.PromiseFactory<Result4>,
pAll.PromiseFactory<Result5>,
pAll.PromiseFactory<Result6>,
pAll.PromiseFactory<Result7>,
pAll.PromiseFactory<Result8>,
pAll.PromiseFactory<Result9>,
pAll.PromiseFactory<Result10>
],
options?: pAll.Options
): Promise<
[
Result1,
Result2,
Result3,
Result4,
Result5,
Result6,
Result7,
Result8,
Result9,
Result10
]
>;
<
Result1,
Result2,
Result3,
Result4,
Result5,
Result6,
Result7,
Result8,
Result9
>(
tasks: [
pAll.PromiseFactory<Result1>,
pAll.PromiseFactory<Result2>,
pAll.PromiseFactory<Result3>,
pAll.PromiseFactory<Result4>,
pAll.PromiseFactory<Result5>,
pAll.PromiseFactory<Result6>,
pAll.PromiseFactory<Result7>,
pAll.PromiseFactory<Result8>,
pAll.PromiseFactory<Result9>
],
options?: pAll.Options
): Promise<
[
Result1,
Result2,
Result3,
Result4,
Result5,
Result6,
Result7,
Result8,
Result9
]
>;
<Result1, Result2, Result3, Result4, Result5, Result6, Result7, Result8>(
tasks: [
pAll.PromiseFactory<Result1>,
pAll.PromiseFactory<Result2>,
pAll.PromiseFactory<Result3>,
pAll.PromiseFactory<Result4>,
pAll.PromiseFactory<Result5>,
pAll.PromiseFactory<Result6>,
pAll.PromiseFactory<Result7>,
pAll.PromiseFactory<Result8>
],
options?: pAll.Options
): Promise<
[Result1, Result2, Result3, Result4, Result5, Result6, Result7, Result8]
>;
<Result1, Result2, Result3, Result4, Result5, Result6, Result7>(
tasks: [
pAll.PromiseFactory<Result1>,
pAll.PromiseFactory<Result2>,
pAll.PromiseFactory<Result3>,
pAll.PromiseFactory<Result4>,
pAll.PromiseFactory<Result5>,
pAll.PromiseFactory<Result6>,
pAll.PromiseFactory<Result7>
],
options?: pAll.Options
): Promise<[Result1, Result2, Result3, Result4, Result5, Result6, Result7]>;
<Result1, Result2, Result3, Result4, Result5, Result6>(
tasks: [
pAll.PromiseFactory<Result1>,
pAll.PromiseFactory<Result2>,
pAll.PromiseFactory<Result3>,
pAll.PromiseFactory<Result4>,
pAll.PromiseFactory<Result5>,
pAll.PromiseFactory<Result6>
],
options?: pAll.Options
): Promise<[Result1, Result2, Result3, Result4, Result5, Result6]>;
<Result1, Result2, Result3, Result4, Result5>(
tasks: [
pAll.PromiseFactory<Result1>,
pAll.PromiseFactory<Result2>,
pAll.PromiseFactory<Result3>,
pAll.PromiseFactory<Result4>,
pAll.PromiseFactory<Result5>
],
options?: pAll.Options
): Promise<[Result1, Result2, Result3, Result4, Result5]>;
<Result1, Result2, Result3, Result4>(
tasks: [
pAll.PromiseFactory<Result1>,
pAll.PromiseFactory<Result2>,
pAll.PromiseFactory<Result3>,
pAll.PromiseFactory<Result4>
],
options?: pAll.Options
): Promise<[Result1, Result2, Result3, Result4]>;
<Result1, Result2, Result3>(
tasks: [
pAll.PromiseFactory<Result1>,
pAll.PromiseFactory<Result2>,
pAll.PromiseFactory<Result3>
],
options?: pAll.Options
): Promise<[Result1, Result2, Result3]>;
<Result1, Result2>(
tasks: [pAll.PromiseFactory<Result1>, pAll.PromiseFactory<Result2>],
options?: pAll.Options
): Promise<[Result1, Result2]>;
<Result1>(
tasks: [pAll.PromiseFactory<Result1>],
options?: pAll.Options
): Promise<[Result1]>;
<TAll>(
tasks: Iterable<pAll.PromiseFactory<TAll>> | pAll.PromiseFactory<TAll>[],
options?: pAll.Options
): Promise<TAll[]>;
// TODO: Remove this for the next major release, refactor the whole definition back to multiple overloaded functions
default: typeof pAll;
};
export = pAll;
+6
View File
@@ -0,0 +1,6 @@
'use strict';
const pMap = require('p-map');
module.exports = (iterable, options) => pMap(iterable, element => element(), options);
// TODO: Remove this for the next major release
module.exports.default = module.exports;
+9
View File
@@ -0,0 +1,9 @@
MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+71
View File
@@ -0,0 +1,71 @@
declare namespace pMap {
interface Options {
/**
Number of concurrently pending promises returned by `mapper`.
@default Infinity
*/
concurrency?: number;
}
/**
Function which is called for every item in `input`. Expected to return a `Promise` or value.
@param input - Iterated element.
@param index - Index of the element in the source array.
*/
type Mapper<Element = any, NewElement = any> = (
input: Element,
index: number
) => NewElement | Promise<NewElement>;
}
declare const pMap: {
/**
Returns a `Promise` that is fulfilled when all promises in `input` and ones returned from `mapper` are fulfilled, or rejects if any of the promises reject. The fulfilled value is an `Array` of the fulfilled values returned from `mapper` in `input` order.
@param input - Iterated over concurrently in the `mapper` function.
@param mapper - Function which is called for every item in `input`. Expected to return a `Promise` or value.
@example
```
import pMap = require('p-map');
import got = require('got');
const sites = [
getWebsiteFromUsername('sindresorhus'), //=> Promise
'ava.li',
'todomvc.com',
'github.com'
];
(async () => {
const mapper = async site => {
const {requestUrl} = await got.head(site);
return requestUrl;
};
const result = await pMap(sites, mapper, {concurrency: 2});
console.log(result);
//=> ['http://sindresorhus.com/', 'http://ava.li/', 'http://todomvc.com/', 'http://github.com/']
})();
```
*/
<Element, NewElement>(
input: Iterable<Element>,
mapper: pMap.Mapper<Element, NewElement>,
options?: pMap.Options
): Promise<NewElement[]>;
// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function pMap<Element, NewElement>(
// input: Iterable<Element>,
// mapper: pMap.Mapper<Element, NewElement>,
// options?: pMap.Options
// ): Promise<NewElement[]>;
// export = pMap;
default: typeof pMap;
};
export = pMap;
+72
View File
@@ -0,0 +1,72 @@
'use strict';
const pMap = (iterable, mapper, options) => new Promise((resolve, reject) => {
options = Object.assign({
concurrency: Infinity
}, options);
if (typeof mapper !== 'function') {
throw new TypeError('Mapper function is required');
}
const {concurrency} = options;
if (!(typeof concurrency === 'number' && concurrency >= 1)) {
throw new TypeError(`Expected \`concurrency\` to be a number from 1 and up, got \`${concurrency}\` (${typeof concurrency})`);
}
const ret = [];
const iterator = iterable[Symbol.iterator]();
let isRejected = false;
let isIterableDone = false;
let resolvingCount = 0;
let currentIndex = 0;
const next = () => {
if (isRejected) {
return;
}
const nextItem = iterator.next();
const i = currentIndex;
currentIndex++;
if (nextItem.done) {
isIterableDone = true;
if (resolvingCount === 0) {
resolve(ret);
}
return;
}
resolvingCount++;
Promise.resolve(nextItem.value)
.then(element => mapper(element, i))
.then(
value => {
ret[i] = value;
resolvingCount--;
next();
},
error => {
isRejected = true;
reject(error);
}
);
};
for (let i = 0; i < concurrency; i++) {
next();
if (isIterableDone) {
break;
}
}
});
module.exports = pMap;
// TODO: Remove this for the next major release
module.exports.default = pMap;
+9
View File
@@ -0,0 +1,9 @@
MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+49
View File
@@ -0,0 +1,49 @@
{
"name": "p-map",
"version": "2.1.0",
"description": "Map over promises concurrently",
"license": "MIT",
"repository": "sindresorhus/p-map",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"promise",
"map",
"resolved",
"wait",
"collection",
"iterable",
"iterator",
"race",
"fulfilled",
"async",
"await",
"promises",
"concurrently",
"concurrency",
"parallel",
"bluebird"
],
"devDependencies": {
"ava": "^1.4.1",
"delay": "^4.1.0",
"in-range": "^1.0.0",
"random-int": "^1.0.0",
"time-span": "^3.1.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
+85
View File
@@ -0,0 +1,85 @@
# p-map [![Build Status](https://travis-ci.org/sindresorhus/p-map.svg?branch=master)](https://travis-ci.org/sindresorhus/p-map)
> Map over promises concurrently
Useful when you need to run promise-returning & async functions multiple times with different inputs concurrently.
## Install
```
$ npm install p-map
```
## Usage
```js
const pMap = require('p-map');
const got = require('got');
const sites = [
getWebsiteFromUsername('sindresorhus'), //=> Promise
'ava.li',
'todomvc.com',
'github.com'
];
(async () => {
const mapper = async site => {
const {requestUrl} = await got.head(site);
return requestUrl;
};
const result = await pMap(sites, mapper, {concurrency: 2});
console.log(result);
//=> ['http://sindresorhus.com/', 'http://ava.li/', 'http://todomvc.com/', 'http://github.com/']
})();
```
## API
### pMap(input, mapper, [options])
Returns a `Promise` that is fulfilled when all promises in `input` and ones returned from `mapper` are fulfilled, or rejects if any of the promises reject. The fulfilled value is an `Array` of the fulfilled values returned from `mapper` in `input` order.
#### input
Type: `Iterable<Promise|any>`
Iterated over concurrently in the `mapper` function.
#### mapper(element, index)
Type: `Function`
Expected to return a `Promise` or value.
#### options
Type: `Object`
##### concurrency
Type: `number`<br>
Default: `Infinity`<br>
Minimum: `1`
Number of concurrently pending promises returned by `mapper`.
## Related
- [p-all](https://github.com/sindresorhus/p-all) - Run promise-returning & async functions concurrently with optional limited concurrency
- [p-filter](https://github.com/sindresorhus/p-filter) - Filter promises concurrently
- [p-times](https://github.com/sindresorhus/p-times) - Run promise-returning & async functions a specific number of times concurrently
- [p-props](https://github.com/sindresorhus/p-props) - Like `Promise.all()` but for `Map` and `Object`
- [p-map-series](https://github.com/sindresorhus/p-map-series) - Map over promises serially
- [p-queue](https://github.com/sindresorhus/p-queue) - Promise queue with concurrency control
- [More…](https://github.com/sindresorhus/promise-fun)
## License
MIT © [Sindre Sorhus](https://sindresorhus.com)
+54
View File
@@ -0,0 +1,54 @@
{
"name": "p-all",
"version": "2.1.0",
"description": "Run promise-returning & async functions concurrently with optional limited concurrency",
"license": "MIT",
"repository": "sindresorhus/p-all",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"promise",
"all",
"function",
"func",
"fn",
"limited",
"limit",
"control",
"rate",
"collection",
"iterable",
"iterator",
"fulfilled",
"async",
"await",
"promises",
"concurrent",
"concurrently",
"concurrency",
"parallel",
"bluebird"
],
"dependencies": {
"p-map": "^2.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"delay": "^4.1.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
+75
View File
@@ -0,0 +1,75 @@
# p-all [![Build Status](https://travis-ci.org/sindresorhus/p-all.svg?branch=master)](https://travis-ci.org/sindresorhus/p-all)
> Run promise-returning & async functions concurrently with optional limited concurrency
Similar to `Promise.all()`, but accepts functions instead of promises directly so you can limit the concurrency.
If you're doing the same work in each function, use [`p-map`](https://github.com/sindresorhus/p-map) instead.
See [`p-series`](https://github.com/sindresorhus/p-series) for a serial counterpart.
## Install
```
$ npm install p-all
```
## Usage
```js
const pAll = require('p-all');
const got = require('got');
(async () => {
const actions = [
() => got('https://sindresorhus.com'),
() => got('https://ava.li'),
() => checkSomething(),
() => doSomethingElse()
];
console.log(await pAll(actions, {concurrency: 2}));
})();
```
## API
### pAll(tasks, [options])
Returns a `Promise` that is fulfilled when all promises returned from calling the functions in `tasks` are fulfilled, or rejects if any of the promises reject. The fulfilled value is an `Array` of the fulfilled values in `tasks` order.
#### tasks
Type: `Iterable<Function>`
Iterable with promise-returning/async functions.
#### options
Type: `Object`
##### concurrency
Type: `number`<br>
Default: `Infinity`<br>
Minimum: `1`
Number of concurrent pending promises.
## Related
- [p-map](https://github.com/sindresorhus/p-map) - Map over promises concurrently
- [p-series](https://github.com/sindresorhus/p-series) - Run promise-returning & async functions in series
- [p-props](https://github.com/sindresorhus/p-props) - Like `Promise.all()` but for `Map` and `Object`
- [p-queue](https://github.com/sindresorhus/p-queue) - Promise queue with concurrency control
- [p-limit](https://github.com/sindresorhus/p-limit) - Run multiple promise-returning & async functions with limited concurrency
- [More…](https://github.com/sindresorhus/promise-fun)
## License
MIT © [Sindre Sorhus](https://sindresorhus.com)