Created
June 14, 2022 16:27
-
-
Save KATT/e752815297db768e624f5eb838870dc7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* @link https://raw.githubusercontent.com/NaturalCycles/js-lib/master/src/promise/pProps.ts | |
* Promise.all for Object instead of Array. | |
* | |
* Inspired by Bluebird Promise.props() and https://github.com/sindresorhus/p-props | |
* | |
* Improvements: | |
* | |
* - Exported as { promiseProps }, so IDE auto-completion works | |
* - Simpler: no support for Map, Mapper, Options | |
* - Included Typescript typings (no need for @types/p-props) | |
* | |
* Concurrency implementation via pMap was removed in favor of preserving async | |
* stack traces (more important!). | |
*/ | |
export async function promiseProps<T>(input: { | |
[K in keyof T]: T[K] | Promise<T[K]>; | |
}): Promise<{ | |
[K in keyof T]: Awaited<T[K]>; | |
}> { | |
const keys = Object.keys(input); | |
return Object.fromEntries( | |
(await Promise.all(Object.values(input))).map((v, i) => [keys[i], v]), | |
) as { | |
[K in keyof T]: Awaited<T[K]>; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment