mirror of
https://github.com/strapi/strapi.git
synced 2025-08-31 04:03:50 +00:00
chore(async-utils): remove reduceAsync
This commit is contained in:
parent
63f4fd8e04
commit
3e532ebd0e
@ -16,7 +16,6 @@ Available functions:
|
|||||||
|
|
||||||
- pipeAsync
|
- pipeAsync
|
||||||
- mapAsync
|
- mapAsync
|
||||||
- reduceAsync
|
|
||||||
|
|
||||||
[See API reference](../../api/Utils) (TODO)
|
[See API reference](../../api/Utils) (TODO)
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const { pipeAsync, mapAsync, reduceAsync } = require('../async');
|
const { pipeAsync, mapAsync } = require('../async');
|
||||||
|
|
||||||
describe('Async utils', () => {
|
describe('Async utils', () => {
|
||||||
describe('pipeAsync', () => {
|
describe('pipeAsync', () => {
|
||||||
@ -77,48 +77,4 @@ describe('Async utils', () => {
|
|||||||
expect(maxOperations).toEqual(2);
|
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 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);
|
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 = {
|
module.exports = {
|
||||||
mapAsync,
|
mapAsync,
|
||||||
reduceAsync,
|
|
||||||
pipeAsync,
|
pipeAsync,
|
||||||
};
|
};
|
||||||
|
@ -37,7 +37,7 @@ const providerFactory = require('./provider-factory');
|
|||||||
const pagination = require('./pagination');
|
const pagination = require('./pagination');
|
||||||
const sanitize = require('./sanitize');
|
const sanitize = require('./sanitize');
|
||||||
const traverseEntity = require('./traverse-entity');
|
const traverseEntity = require('./traverse-entity');
|
||||||
const { pipeAsync, mapAsync, reduceAsync } = require('./async');
|
const { pipeAsync, mapAsync } = require('./async');
|
||||||
const convertQueryParams = require('./convert-query-params');
|
const convertQueryParams = require('./convert-query-params');
|
||||||
const importDefault = require('./import-default');
|
const importDefault = require('./import-default');
|
||||||
const template = require('./template');
|
const template = require('./template');
|
||||||
@ -82,7 +82,6 @@ module.exports = {
|
|||||||
pagination,
|
pagination,
|
||||||
pipeAsync,
|
pipeAsync,
|
||||||
mapAsync,
|
mapAsync,
|
||||||
reduceAsync,
|
|
||||||
errors,
|
errors,
|
||||||
validateYupSchema,
|
validateYupSchema,
|
||||||
validateYupSchemaSync,
|
validateYupSchemaSync,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user