mirror of
https://github.com/strapi/strapi.git
synced 2025-08-30 11:45:48 +00:00
chore(async-utils): remove reduceAsync
This commit is contained in:
parent
63f4fd8e04
commit
3e532ebd0e
@ -16,7 +16,6 @@ Available functions:
|
||||
|
||||
- pipeAsync
|
||||
- mapAsync
|
||||
- reduceAsync
|
||||
|
||||
[See API reference](../../api/Utils) (TODO)
|
||||
|
||||
|
@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
10
packages/core/utils/lib/async.d.ts
vendored
10
packages/core/utils/lib/async.d.ts
vendored
@ -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>;
|
||||
|
@ -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,
|
||||
};
|
||||
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user