feat(async-utils): use curryN instead of curry

This commit is contained in:
nathan-pichon 2023-02-14 16:55:57 +01:00
parent 8fae1a80ec
commit 5029344805
No known key found for this signature in database
2 changed files with 14 additions and 4 deletions

View File

@ -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 () => {

View File

@ -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 }