diff --git a/src/async/parallel.ts b/src/async/parallel.ts index 42b42e7b..05b0c4dd 100644 --- a/src/async/parallel.ts +++ b/src/async/parallel.ts @@ -66,6 +66,10 @@ export async function parallel( array: readonly T[], func: (item: T) => Promise, ): Promise { + if (array.length === 0) { + return [] + } + const work = array.map((item, index) => ({ index, item, diff --git a/tests/async/parallel.test.ts b/tests/async/parallel.test.ts index a84ad4bb..df95d2df 100644 --- a/tests/async/parallel.test.ts +++ b/tests/async/parallel.test.ts @@ -134,4 +134,11 @@ describe('parallel', () => { test('limit defaults to array length if Infinity is passed', async () => { await testConcurrency(Number.POSITIVE_INFINITY, _.list(1, 3), [1, 2, 3]) }) + + test('returns empty array for empty input', async () => { + const result = await _.parallel(1, [], async () => { + throw new Error('Should not be called') + }) + expect(result).toEqual([]) + }) })