Skip to content

Instantly share code, notes, and snippets.

@lukeggchapman
Created September 3, 2019 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukeggchapman/ffa5a5e57eae5a7649ec0b24f1117677 to your computer and use it in GitHub Desktop.
Save lukeggchapman/ffa5a5e57eae5a7649ec0b24f1117677 to your computer and use it in GitHub Desktop.
Typescript promiseAllObj. Like Promise.all but for objects. Similar to bluebird.props.
// Like Promise.all, but for objects of {key: Promise}
const promsieAllObj = async <T>(obj: Record<string, Promise<T>>): Promise<Record<string, T>> => {
const arr = Object.entries(obj).map(([key, value]) => value.then(res => [key, res]));
return Object.assign(
{},
...(await Promise.all(arr).then(res => res.map(([k, v]) => ({ [k as string]: v as T }))))
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment