-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add grouping of promise results by status
- Loading branch information
1 parent
cf4d9e3
commit 5f50e8e
Showing
4 changed files
with
54 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export function groupByStatus<T>(results: PromiseSettledResult<T>[]): { | ||
fulfilled: PromiseFulfilledResult<T>[]; | ||
rejected: PromiseRejectedResult[]; | ||
} { | ||
return results.reduce<{ | ||
fulfilled: PromiseFulfilledResult<T>[]; | ||
rejected: PromiseRejectedResult[]; | ||
}>( | ||
(accumulator, currentValue) => { | ||
if (currentValue.status === 'fulfilled' && 'value' in currentValue) { | ||
accumulator.fulfilled.push(currentValue); | ||
} else if ( | ||
currentValue.status === 'rejected' && | ||
'reason' in currentValue | ||
) { | ||
accumulator.rejected.push(currentValue); | ||
} | ||
return accumulator; | ||
}, | ||
{ fulfilled: [], rejected: [] }, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { describe } from 'vitest'; | ||
import { groupByStatus } from './group-by-status'; | ||
|
||
describe('groupByStatus', () => { | ||
it('should group results by status', () => { | ||
const results = [ | ||
{ status: 'fulfilled', value: 'first' }, | ||
{ status: 'rejected', reason: 'second' }, | ||
{ status: 'fulfilled', value: 'third' }, | ||
] as PromiseSettledResult<string>[]; | ||
const grouped = groupByStatus(results); | ||
expect(grouped).toEqual({ | ||
fulfilled: [ | ||
{ status: 'fulfilled', value: 'first' }, | ||
{ status: 'fulfilled', value: 'third' }, | ||
], | ||
rejected: [{ status: 'rejected', reason: 'second' }], | ||
}); | ||
}); | ||
}); |