[d24f17c] | 1 | import { curryN, reduce, length } from 'ramda';
|
---|
| 2 |
|
---|
| 3 | import isUndefined from './isUndefined';
|
---|
| 4 | import resolveP from './resolveP';
|
---|
| 5 | import allP from './allP';
|
---|
| 6 |
|
---|
| 7 | /* eslint-disable max-len */
|
---|
| 8 | /**
|
---|
| 9 | * Given an `Iterable`(arrays are `Iterable`), or a promise of an `Iterable`,
|
---|
| 10 | * which produces promises (or a mix of promises and values),
|
---|
| 11 | * iterate over all the values in the `Iterable` into an array and
|
---|
| 12 | * reduce the array to a value using the given iterator function.
|
---|
| 13 | *
|
---|
| 14 | * If the iterator function returns a promise, then the result of the promise is awaited,
|
---|
| 15 | * before continuing with next iteration. If any promise in the array is rejected or a promise
|
---|
| 16 | * returned by the iterator function is rejected, the result is rejected as well.
|
---|
| 17 | *
|
---|
| 18 | * If `initialValue` is `undefined` (or a promise that resolves to `undefined`) and
|
---|
| 19 | * the `Iterable` contains only 1 item, the callback will not be called and
|
---|
| 20 | * the `Iterable's` single item is returned. If the `Iterable` is empty, the callback
|
---|
| 21 | * will not be called and `initialValue` is returned (which may be undefined).
|
---|
| 22 | *
|
---|
| 23 | * This function is basically equivalent to {@link http://bluebirdjs.com/docs/api/promise.reduce.html|bluebird.reduce}.
|
---|
| 24 | *
|
---|
| 25 | * @func reduceP
|
---|
| 26 | * @memberOf RA
|
---|
| 27 | * @since {@link https://char0n.github.io/ramda-adjunct/1.13.0|v1.13.0}
|
---|
| 28 | * @category List
|
---|
| 29 | * @typedef MaybePromise = Promise.<*> | *
|
---|
| 30 | * @sig ((Promise a, MaybePromise b) -> Promise a) -> MaybePromise a -> MaybePromise [MaybePromise b] -> Promise a
|
---|
| 31 | * @param {Function} fn The iterator function. Receives two values, the accumulator and the current element from the list
|
---|
| 32 | * @param {*|Promise.<*>} acc The accumulator value
|
---|
| 33 | * @param {Array.<*>|Promise.<Array<*|Promise.<*>>>} list The list to iterate over
|
---|
| 34 | * @return {Promise} The final, accumulated value
|
---|
| 35 | * @see {@link http://ramdajs.com/docs/#reduce|R.reduce}, {@link RA.reduceRightP|reduceRightP}, {@link http://bluebirdjs.com/docs/api/promise.reduce.html|bluebird.reduce}
|
---|
| 36 | * @example
|
---|
| 37 | *
|
---|
| 38 | * RA.reduceP(
|
---|
| 39 | * (total, fileName) => fs
|
---|
| 40 | * .readFileAsync(fileName, 'utf8')
|
---|
| 41 | * .then(contents => total + parseInt(contents, 10)),
|
---|
| 42 | * 0,
|
---|
| 43 | * ['file1.txt', 'file2.txt', 'file3.txt']
|
---|
| 44 | * ); // => Promise(10)
|
---|
| 45 | *
|
---|
| 46 | * RA.reduceP(
|
---|
| 47 | * (total, fileName) => fs
|
---|
| 48 | * .readFileAsync(fileName, 'utf8')
|
---|
| 49 | * .then(contents => total + parseInt(contents, 10)),
|
---|
| 50 | * Promise.resolve(0),
|
---|
| 51 | * ['file1.txt', 'file2.txt', 'file3.txt']
|
---|
| 52 | * ); // => Promise(10)
|
---|
| 53 | *
|
---|
| 54 | * RA.reduceP(
|
---|
| 55 | * (total, fileName) => fs
|
---|
| 56 | * .readFileAsync(fileName, 'utf8')
|
---|
| 57 | * .then(contents => total + parseInt(contents, 10)),
|
---|
| 58 | * 0,
|
---|
| 59 | * [Promise.resolve('file1.txt'), 'file2.txt', 'file3.txt']
|
---|
| 60 | * ); // => Promise(10)
|
---|
| 61 | *
|
---|
| 62 | * RA.reduceP(
|
---|
| 63 | * (total, fileName) => fs
|
---|
| 64 | * .readFileAsync(fileName, 'utf8')
|
---|
| 65 | * .then(contents => total + parseInt(contents, 10)),
|
---|
| 66 | * 0,
|
---|
| 67 | * Promise.resolve([Promise.resolve('file1.txt'), 'file2.txt', 'file3.txt'])
|
---|
| 68 | * ); // => Promise(10)
|
---|
| 69 | *
|
---|
| 70 | */
|
---|
| 71 | /* esline-enable max-len */
|
---|
| 72 | const reduceP = curryN(3, (fn, acc, list) =>
|
---|
| 73 | resolveP(list).then((iterable) => {
|
---|
| 74 | const listLength = length(iterable);
|
---|
| 75 |
|
---|
| 76 | if (listLength === 0) {
|
---|
| 77 | return acc;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | const reducer = reduce((accP, currentValueP) =>
|
---|
| 81 | accP
|
---|
| 82 | .then((previousValue) => allP([previousValue, currentValueP]))
|
---|
| 83 | .then(([previousValue, currentValue]) => {
|
---|
| 84 | if (isUndefined(previousValue) && listLength === 1) {
|
---|
| 85 | return currentValue;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | return fn(previousValue, currentValue);
|
---|
| 89 | })
|
---|
| 90 | );
|
---|
| 91 |
|
---|
| 92 | return reducer(resolveP(acc), iterable);
|
---|
| 93 | })
|
---|
| 94 | );
|
---|
| 95 |
|
---|
| 96 | export default reduceP;
|
---|