Skip to content

Commit

Permalink
fix(parallel): clamp error with empty array
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Jan 7, 2025
1 parent 3426478 commit 8d99e9e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/async/parallel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export async function parallel<T, K>(
array: readonly T[],
func: (item: T) => Promise<K>,
): Promise<K[]> {
if (array.length === 0) {
return []
}

const work = array.map((item, index) => ({
index,
item,
Expand Down
7 changes: 7 additions & 0 deletions tests/async/parallel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([])
})
})

0 comments on commit 8d99e9e

Please sign in to comment.