Created
September 3, 2019 04:58
-
-
Save lukeggchapman/ffa5a5e57eae5a7649ec0b24f1117677 to your computer and use it in GitHub Desktop.
Typescript promiseAllObj. Like Promise.all but for objects. Similar to bluebird.props.
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
// 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