Skip to content

Instantly share code, notes, and snippets.

@pokey
Forked from d0mmie/PromiseAllObject.js
Last active September 25, 2020 11:49
Show Gist options
  • Save pokey/1a9b27e6c2aedfce0327c1480772a9ac to your computer and use it in GitHub Desktop.
Save pokey/1a9b27e6c2aedfce0327c1480772a9ac to your computer and use it in GitHub Desktop.
Object version of promiseAll
type PromiseAllObjectInput<T> = {
[P in keyof T]: Promise<T[P]>;
};
export async function promiseAllObject<T>(
obj: PromiseAllObjectInput<T>,
): Promise<T> {
const promiseArray = await Promise.all(Object.values(obj));
return Object.keys(obj)
.map((key, index) => ({key, index}))
.reduce(
(total, {key, index}) => ({...total, [key]: promiseArray[index]}),
{},
) as T;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment