From 3e532ebd0eba6f9d46f968111937c29e2dbbcd58 Mon Sep 17 00:00:00 2001 From: nathan-pichon Date: Fri, 27 Jan 2023 16:04:55 +0100 Subject: [PATCH] chore(async-utils): remove reduceAsync --- docs/docs/core/utils/async.md | 1 - .../core/utils/lib/__tests__/async.test.js | 46 +------------------ packages/core/utils/lib/async.d.ts | 10 ---- packages/core/utils/lib/async.js | 12 ----- packages/core/utils/lib/index.js | 3 +- 5 files changed, 2 insertions(+), 70 deletions(-) diff --git a/docs/docs/core/utils/async.md b/docs/docs/core/utils/async.md index 6fef0e37e0..3dcb56be2f 100644 --- a/docs/docs/core/utils/async.md +++ b/docs/docs/core/utils/async.md @@ -16,7 +16,6 @@ Available functions: - pipeAsync - mapAsync -- reduceAsync [See API reference](../../api/Utils) (TODO) diff --git a/packages/core/utils/lib/__tests__/async.test.js b/packages/core/utils/lib/__tests__/async.test.js index 1cae0186ee..74f9f53ea2 100644 --- a/packages/core/utils/lib/__tests__/async.test.js +++ b/packages/core/utils/lib/__tests__/async.test.js @@ -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'); - }); - }); }); diff --git a/packages/core/utils/lib/async.d.ts b/packages/core/utils/lib/async.d.ts index d1e52caf6d..6f5b813800 100644 --- a/packages/core/utils/lib/async.d.ts +++ b/packages/core/utils/lib/async.d.ts @@ -1,11 +1 @@ -import * as pMap from "p-map"; - -type PromiseArray = (T | Promise)[]; - -type MapIteratee = (value: T, index: number) => R | Promise; - -type ReduceIteratee = (previousResult: P, currentValue: C, index: number) => R | Promise; - export type MapAsync = lodash.CurriedFunction3 R | Promise, { concurrency?: number }, Promise>; - -export declare function reduceAsync(promiseArray: PromiseArray): (iteratee: ReduceIteratee, initialValue?: I) => Promise; diff --git a/packages/core/utils/lib/async.js b/packages/core/utils/lib/async.js index 9c14998297..4ceb986a5b 100644 --- a/packages/core/utils/lib/async.js +++ b/packages/core/utils/lib/async.js @@ -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, }; diff --git a/packages/core/utils/lib/index.js b/packages/core/utils/lib/index.js index 29a9d1a1de..ed6fe09048 100644 --- a/packages/core/utils/lib/index.js +++ b/packages/core/utils/lib/index.js @@ -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,