Skip to content

Instantly share code, notes, and snippets.

Created July 10, 2017 20:17
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Like _.mapValues, but asynchronous
import mapAsync from './mapAsync';
interface Dict<TVal> {
[key: string]: TVal
async function mapValuesAsync<TVal, TRes>(obj:Dict<TVal>, iter:(val:TVal, key:string) => Promise<TRes>):Promise<Dict<TRes>> {
const keyResPairs:[string, TRes][] = await mapAsync<string, [string, TRes]>(Object.keys(obj),
async (key:string) => {
const res = await iter(obj[key], key);
return [key, res];
return keyResPairs.reduce((obj:Dict<TRes>, [key, res]:[string, TRes]) => ({
[key]: res
}), {});
export default mapValuesAsync;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment