Installing dependencies.
This commit is contained in:
+57
@@ -0,0 +1,57 @@
|
||||
declare namespace pReduce {
|
||||
type ReducerFunction<ValueType, ReducedValueType = ValueType> = (
|
||||
previousValue: ReducedValueType,
|
||||
currentValue: ValueType,
|
||||
index: number
|
||||
) => PromiseLike<ReducedValueType> | ReducedValueType;
|
||||
}
|
||||
|
||||
declare const pReduce: {
|
||||
/**
|
||||
Reduce a list of values using promises into a promise for a value.
|
||||
|
||||
@param input - Iterated over serially in the `reducer` function.
|
||||
@param reducer - Expected to return a value. If a `Promise` is returned, it's awaited before continuing with the next iteration.
|
||||
@param initialValue - Value to use as `previousValue` in the first `reducer` invocation.
|
||||
@returns A `Promise` that is fulfilled when all promises in `input` and ones returned from `reducer` are fulfilled, or rejects if any of the promises reject. The resolved value is the result of the reduction.
|
||||
|
||||
@example
|
||||
```
|
||||
import pReduce = require('p-reduce');
|
||||
import humanInfo from 'human-info'; // Not a real module
|
||||
|
||||
(async () => {
|
||||
const names = [
|
||||
getUser('sindresorhus').then(info => info.name),
|
||||
'Addy Osmani',
|
||||
'Pascal Hartig',
|
||||
'Stephen Sawchuk'
|
||||
];
|
||||
|
||||
const totalAge = await pReduce(names, async (total, name) => {
|
||||
const info = await humanInfo(name);
|
||||
return total + info.age;
|
||||
}, 0);
|
||||
|
||||
console.log(totalAge);
|
||||
//=> 125
|
||||
})();
|
||||
```
|
||||
*/
|
||||
<ValueType, ReducedValueType = ValueType>(
|
||||
input: Iterable<PromiseLike<ValueType> | ValueType>,
|
||||
reducer: pReduce.ReducerFunction<ValueType, ReducedValueType>,
|
||||
initialValue?: ReducedValueType
|
||||
): Promise<ReducedValueType>;
|
||||
|
||||
// TODO: Remove this for the next major release, refactor the whole definition to:
|
||||
// declare function pReduce<ValueType, ReducedValueType = ValueType>(
|
||||
// input: Iterable<PromiseLike<ValueType> | ValueType>,
|
||||
// reducer: pReduce.ReducerFunction<ValueType, ReducedValueType>,
|
||||
// initialValue?: ReducedValueType
|
||||
// ): Promise<ReducedValueType>;
|
||||
// export = pReduce;
|
||||
default: typeof pReduce;
|
||||
};
|
||||
|
||||
export = pReduce;
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
'use strict';
|
||||
|
||||
const pReduce = (iterable, reducer, initialValue) => new Promise((resolve, reject) => {
|
||||
const iterator = iterable[Symbol.iterator]();
|
||||
let index = 0;
|
||||
|
||||
const next = async total => {
|
||||
const element = iterator.next();
|
||||
|
||||
if (element.done) {
|
||||
resolve(total);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const value = await Promise.all([total, element.value]);
|
||||
next(reducer(value[0], value[1], index++));
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
};
|
||||
|
||||
next(initialValue);
|
||||
});
|
||||
|
||||
module.exports = pReduce;
|
||||
// TODO: Remove this for the next major release
|
||||
module.exports.default = pReduce;
|
||||
+9
@@ -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.
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"name": "p-reduce",
|
||||
"version": "2.1.0",
|
||||
"description": "Reduce a list of values using promises into a promise for a value",
|
||||
"license": "MIT",
|
||||
"repository": "sindresorhus/p-reduce",
|
||||
"author": {
|
||||
"name": "Sindre Sorhus",
|
||||
"email": "sindresorhus@gmail.com",
|
||||
"url": "sindresorhus.com"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && ava && tsd"
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.d.ts"
|
||||
],
|
||||
"keywords": [
|
||||
"promise",
|
||||
"reduce",
|
||||
"collection",
|
||||
"iterable",
|
||||
"iterator",
|
||||
"async",
|
||||
"await",
|
||||
"promises",
|
||||
"accumulate",
|
||||
"bluebird"
|
||||
],
|
||||
"devDependencies": {
|
||||
"ava": "^1.4.1",
|
||||
"delay": "^4.1.0",
|
||||
"tsd": "^0.7.2",
|
||||
"xo": "^0.24.0"
|
||||
}
|
||||
}
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
# p-reduce [](https://travis-ci.org/sindresorhus/p-reduce)
|
||||
|
||||
> Reduce a list of values using promises into a promise for a value
|
||||
|
||||
Useful when you need to calculate some accumulated value based on async resources.
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
$ npm install p-reduce
|
||||
```
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const pReduce = require('p-reduce');
|
||||
const humanInfo = require('human-info'); // Not a real module
|
||||
|
||||
(async () => {
|
||||
const names = [
|
||||
getUser('sindresorhus').then(info => info.name),
|
||||
'Addy Osmani',
|
||||
'Pascal Hartig',
|
||||
'Stephen Sawchuk'
|
||||
];
|
||||
|
||||
const totalAge = await pReduce(names, async (total, name) => {
|
||||
const info = await humanInfo(name);
|
||||
return total + info.age;
|
||||
}, 0);
|
||||
|
||||
console.log(totalAge);
|
||||
//=> 125
|
||||
})();
|
||||
```
|
||||
|
||||
|
||||
## API
|
||||
|
||||
### pReduce(input, reducer, [initialValue])
|
||||
|
||||
Returns a `Promise` that is fulfilled when all promises in `input` and ones returned from `reducer` are fulfilled, or rejects if any of the promises reject. The fulfilled value is the result of the reduction.
|
||||
|
||||
#### input
|
||||
|
||||
Type: `Iterable<Promise|any>`
|
||||
|
||||
Iterated over serially in the `reducer` function.
|
||||
|
||||
#### reducer(previousValue, currentValue, index)
|
||||
|
||||
Type: `Function`
|
||||
|
||||
Expected to return a value. If a `Promise` is returned, it's awaited before continuing with the next iteration.
|
||||
|
||||
#### initialValue
|
||||
|
||||
Type: `unknown`
|
||||
|
||||
Value to use as `previousValue` in the first `reducer` invocation.
|
||||
|
||||
|
||||
## Related
|
||||
|
||||
- [p-each-series](https://github.com/sindresorhus/p-each-series) - Iterate over promises serially
|
||||
- [p-map-series](https://github.com/sindresorhus/p-map-series) - Map over promises serially
|
||||
- [p-map](https://github.com/sindresorhus/p-map) - Map over promises concurrently
|
||||
- [More…](https://github.com/sindresorhus/promise-fun)
|
||||
|
||||
|
||||
## License
|
||||
|
||||
MIT © [Sindre Sorhus](https://sindresorhus.com)
|
||||
Reference in New Issue
Block a user