fix(async): arity for reduce func and awaited value

This commit is contained in:
nathan-pichon 2023-02-13 11:33:03 +01:00
parent 5b0e7f6903
commit 477750650e
No known key found for this signature in database
3 changed files with 8 additions and 6 deletions

View File

@ -108,7 +108,7 @@ describe('Async utils', () => {
await expect(async () => {
await reduceFunc(() => {
throw new Error('test');
});
}, null);
}).rejects.toThrow('test');
});
test('Should throw an error 2', async () => {
@ -117,7 +117,7 @@ describe('Async utils', () => {
const reduceFunc = reduceAsync(numberPromiseArray);
await expect(async () => {
await reduceFunc(() => true);
await reduceFunc(() => true, null);
}).rejects.toThrow('input');
});
});

View File

@ -1,4 +1,6 @@
export type MapAsync<T = any, R = any> = lodash.CurriedFunction3<
import { CurriedFunction3 } from 'lodash';
export type MapAsync<T = any, R = any> = CurriedFunction3<
T[],
(element: T, index: number) => R | Promise<R>,
{ concurrency?: number },
@ -11,7 +13,7 @@ export type ForEachAsync<T = any, R = any> = (
options?: { concurrency?: number }
) => Promise<R[]>;
export type ReduceAsync<T = any, V = T, R = V> = lodash.CurriedFunction3<
export type ReduceAsync<T = any, V = T, R = V> = CurriedFunction3<
T[],
(accumulator: V | R, current: Awaited<T>, index: number) => R | Promise<R>,
V,

View File

@ -26,10 +26,10 @@ const mapAsync = curry(pMap);
const reduceAsync = curry(async (mixedArray, iteratee, initialValue) => {
let acc = initialValue;
for (let i = 0; i < mixedArray.length; i += 1) {
acc = await iteratee(acc, mixedArray[i], i);
acc = await iteratee(acc, await mixedArray[i], i);
}
return acc;
});
}, 2);
/**
* @type { import('./async').ForEachAsync }