chore(async-utils): remove reduceAsync

This commit is contained in:
nathan-pichon 2023-01-27 16:04:55 +01:00
parent 63f4fd8e04
commit 3e532ebd0e
No known key found for this signature in database
5 changed files with 2 additions and 70 deletions

View File

@ -16,7 +16,6 @@ Available functions:
- pipeAsync
- mapAsync
- reduceAsync
[See API reference](../../api/Utils) (TODO)

View File

@ -1,6 +1,6 @@
'use strict';
const { pipeAsync, mapAsync, reduceAsync } = require('../async');
const { pipeAsync, mapAsync } = require('../async');
describe('Async utils', () => {
describe('pipeAsync', () => {
@ -77,48 +77,4 @@ describe('Async utils', () => {
expect(maxOperations).toEqual(2);
});
});
describe('reduceAsync', () => {
test('Should return a incremented number', async () => {
const numberPromiseArray = [Promise.resolve(1), Promise.resolve(2)];
const reduceFunc = reduceAsync(numberPromiseArray);
const result = await reduceFunc(
(previousValue, currentValue) => previousValue + currentValue,
10
);
expect(result).toEqual(13);
});
test('Should work with mix of promises and values', async () => {
const numberMixArray = [1, Promise.resolve(2)];
const reduceFunc = reduceAsync(numberMixArray);
const result = await reduceFunc(
(previousValue, currentValue) => previousValue + currentValue,
10
);
expect(result).toEqual(13);
});
test('Should throw an error', async () => {
const numberPromiseArray = [Promise.resolve(1), Promise.resolve(2)];
const reduceFunc = reduceAsync(numberPromiseArray);
await expect(async () => {
await reduceFunc(() => {
throw new Error('test');
});
}).rejects.toThrow('test');
});
test('Should throw an error 2', async () => {
const numberPromiseArray = [Promise.reject(new Error('input')), Promise.resolve(2)];
const reduceFunc = reduceAsync(numberPromiseArray);
await expect(async () => {
await reduceFunc(() => true);
}).rejects.toThrow('input');
});
});
});

View File

@ -1,11 +1 @@
import * as pMap from "p-map";
type PromiseArray<T> = (T | Promise<T>)[];
type MapIteratee<T, R = T> = (value: T, index: number) => R | Promise<R>;
type ReduceIteratee<P, C = P, R = P> = (previousResult: P, currentValue: C, index: number) => R | Promise<R>;
export type MapAsync<T = any, R = any> = lodash.CurriedFunction3<T[], (element: T, index: number) => R | Promise<R>, { concurrency?: number }, Promise<R[]>>;
export declare function reduceAsync<T = unknown>(promiseArray: PromiseArray<T>): <R = unknown, I>(iteratee: ReduceIteratee<I | R, T, R>, initialValue?: I) => Promise<R>;

View File

@ -20,19 +20,7 @@ function pipeAsync(...methods) {
*/
const mapAsync = curry(pMap);
/**
* @type { import('./async').reduceAsync }
*/
function reduceAsync(promiseArray) {
return (iteratee, initialValue) =>
promiseArray.reduce(async (previousPromise, currentValue, index) => {
const previousValue = await previousPromise;
return iteratee(previousValue, await currentValue, index);
}, Promise.resolve(initialValue));
}
module.exports = {
mapAsync,
reduceAsync,
pipeAsync,
};

View File

@ -37,7 +37,7 @@ const providerFactory = require('./provider-factory');
const pagination = require('./pagination');
const sanitize = require('./sanitize');
const traverseEntity = require('./traverse-entity');
const { pipeAsync, mapAsync, reduceAsync } = require('./async');
const { pipeAsync, mapAsync } = require('./async');
const convertQueryParams = require('./convert-query-params');
const importDefault = require('./import-default');
const template = require('./template');
@ -82,7 +82,6 @@ module.exports = {
pagination,
pipeAsync,
mapAsync,
reduceAsync,
errors,
validateYupSchema,
validateYupSchemaSync,