Created
January 28, 2018 17:24
-
-
Save jcsrb/c9fd5d2928b4341b120d6db375679095 to your computer and use it in GitHub Desktop.
Lodash's .mapValues but for async functions
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
const mapValuesAsync = (obj, asyncFn) => { | |
const keys = Object.keys(obj); | |
const promises = keys.map(k => { | |
return asyncFn(obj[k]).then(newValue => { | |
return {key: k, value: newValue}; | |
}); | |
}); | |
return Promise.all(promises).then(values => { | |
const newObj = {}; | |
values.forEach(v => { | |
newObj[v.key] = v.value; | |
}); | |
return newObj; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I suggest you my own version :