-
-
Save pokey/1a9b27e6c2aedfce0327c1480772a9ac to your computer and use it in GitHub Desktop.
Object version of promiseAll
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
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