Created
July 25, 2017 22:02
-
-
Save klimashkin/7bbbea885f5b4a556512035934fdaa52 to your computer and use it in GitHub Desktop.
Native implementation of Bluebird's Promise.props with flow annotation
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
function props<A, O: { [key: string]: A }>(promises: O): Promise<$ObjMap<O, typeof $await>> { | |
const objMapping: {[number]: string} = {}; | |
return Promise | |
.all(Object.keys(promises).map((key: string, index: number) => { | |
objMapping[index] = key; | |
return promises[key]; | |
})) | |
.then(fulfilledArray => { | |
return fulfilledArray.reduce((result, value, index: number) => { | |
result[objMapping[index]] = value; | |
return result; | |
}, {}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment