-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from giraugh/feat/resolve-object-settled
Add a settleObject util
- Loading branch information
Showing
5 changed files
with
82 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@giraugh/tools": minor | ||
--- | ||
|
||
Add a `settleObject` util that behaves like Promise.allSettled for objects |
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,5 @@ | ||
--- | ||
"@giraugh/tools": patch | ||
--- | ||
|
||
Allow `resolveObject` to take a `number` or `symbol` as a key |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './filterAsync' | ||
export * from './mapAsync' | ||
export * from './resolveObject' | ||
export * from './settleObject' |
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,61 @@ | ||
/** | ||
* Settle all of the fields of an object in parallel | ||
* @param object An object where every field is a promise | ||
* @returns The same object with every field settled | ||
* | ||
* @example await settleObject({ | ||
* a: Promise.resolve('a'), | ||
* b: Promise.resolve('b'), | ||
* }) === { | ||
* a: { status: 'fulfilled', value: 'a' }, | ||
* b: { status: 'fulfilled', value: 'b' }, | ||
* } | ||
* | ||
* @example await settleObject({ | ||
* a: Promise.resolve('a'), | ||
* b: Promise.reject('b'), | ||
* }) === { | ||
* a: { status: 'fulfilled', value: 'a' }, | ||
* b: { status: 'rejected', reason: 'b' }, | ||
* } | ||
* | ||
* @see {@link resolveObject} if you want to fail on any promise rejection | ||
*/ | ||
export const settleObject = async <T extends Record<PropertyKey, Promise<unknown>>>(object: T): Promise<{ [k in keyof T]: PromiseSettledResult<T[k]> }> => { | ||
// Turn into entries | ||
const entries = Object.entries(object) | ||
|
||
// Settle the promises | ||
const settledValues = await Promise.allSettled(entries.map(e => e[1])) | ||
|
||
// Match keys with settled values | ||
const settledEntries = settledValues.map((v, i) => [entries[i][0], v] as const) | ||
|
||
// Reconstruct object | ||
return Object.fromEntries(settledEntries) as { [k in keyof T]: PromiseSettledResult<T[k]> } | ||
} | ||
|
||
// Tests | ||
if (import.meta.vitest) { | ||
const { it, expect } = import.meta.vitest | ||
|
||
it('works for example 1', () => { | ||
expect(settleObject({ | ||
a: Promise.resolve('a'), | ||
b: Promise.resolve('b'), | ||
})).resolves.toStrictEqual({ | ||
a: { status: 'fulfilled', value: 'a' }, | ||
b: { status: 'fulfilled', value: 'b' }, | ||
}) | ||
}) | ||
|
||
it('works for example 2', () => { | ||
expect(settleObject({ | ||
a: Promise.resolve('a'), | ||
b: Promise.reject('b'), | ||
})).resolves.toStrictEqual({ | ||
a: { status: 'fulfilled', value: 'a' }, | ||
b: { status: 'rejected', reason: 'b' }, | ||
}) | ||
}) | ||
} |