mirror of
https://github.com/strapi/strapi.git
synced 2025-12-27 23:24:03 +00:00
feat(async-utils): use curryN instead of curry
This commit is contained in:
parent
8fae1a80ec
commit
5029344805
@ -89,6 +89,16 @@ describe('Async utils', () => {
|
||||
|
||||
expect(result).toEqual(13);
|
||||
});
|
||||
test('Should work without initial value', async () => {
|
||||
const numberPromiseArray = [Promise.resolve(1), Promise.resolve(2)];
|
||||
|
||||
const reduceFunc = reduceAsync(numberPromiseArray);
|
||||
const result = await reduceFunc(
|
||||
(previousValue, currentValue) => (previousValue || 10) + currentValue
|
||||
);
|
||||
|
||||
expect(result).toEqual(13);
|
||||
});
|
||||
test('Should work with mix of promises and values', async () => {
|
||||
const numberMixArray = [1, Promise.resolve(2)];
|
||||
|
||||
@ -108,7 +118,7 @@ describe('Async utils', () => {
|
||||
await expect(async () => {
|
||||
await reduceFunc(() => {
|
||||
throw new Error('test');
|
||||
}, null);
|
||||
});
|
||||
}).rejects.toThrow('test');
|
||||
});
|
||||
test('Should throw an error with proper message when the input array contains a rejected Promise', async () => {
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const pMap = require('p-map');
|
||||
const { curry } = require('lodash/fp');
|
||||
const { curry, curryN } = require('lodash/fp');
|
||||
|
||||
function pipeAsync(...methods) {
|
||||
return async (data) => {
|
||||
@ -23,13 +23,13 @@ const mapAsync = curry(pMap);
|
||||
/**
|
||||
* @type { import('./async').ReduceAsync }
|
||||
*/
|
||||
const reduceAsync = curry(async (mixedArray, iteratee, initialValue) => {
|
||||
const reduceAsync = curryN(2, async (mixedArray, iteratee, initialValue) => {
|
||||
let acc = initialValue;
|
||||
for (let i = 0; i < mixedArray.length; i += 1) {
|
||||
acc = await iteratee(acc, await mixedArray[i], i);
|
||||
}
|
||||
return acc;
|
||||
}, 2);
|
||||
});
|
||||
|
||||
/**
|
||||
* @type { import('./async').ForEachAsync }
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user