Skip to content

Instantly share code, notes, and snippets.

@andycarrell
Last active June 14, 2022 19:31
Show Gist options
  • Save andycarrell/7f521ce8e54e114c9345f7288fbb4346 to your computer and use it in GitHub Desktop.
Save andycarrell/7f521ce8e54e114c9345f7288fbb4346 to your computer and use it in GitHub Desktop.
/**
* @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!).
*/
type InputProps<T> = {
[K in keyof T]: T[K] | Promise<T[K]>;
};
type ReturnProps<T> = {
[K in keyof T]: Awaited<T[K]>;
};
export async function promiseProps<T>(input: InputProps<T>): Promise<ReturnProps<T>> {
return Object.fromEntries(
await Promise.all(
Object.entries(input).map(async ([key, prop]) => [key, await prop])
)
) as ReturnProps<T>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment