Created
January 28, 2022 17:43
-
-
Save vivmaha/14f90d30f53fc27efa2f386147388b0a to your computer and use it in GitHub Desktop.
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
import pMap from "p-map"; | |
import { invokeLambdaAsync, invokeLambdaSync } from "./lambda-utils-aws"; | |
export const mapReduce = async <TItem, TMap, TReduce>(options: { | |
items: TItem[]; | |
map: { | |
functionName: string; | |
getPayloadForItem: (item: TItem) => TMap; | |
concurrency: number; | |
}; | |
reduce: { | |
functionName: string; | |
payload: TReduce; | |
}; | |
}): Promise<void> => { | |
const { items, map, reduce } = options; | |
// The map part | |
await pMap( | |
items, | |
async (item) => { | |
await invokeLambdaSync<TMap>( | |
map.functionName, | |
map.getPayloadForItem(item) | |
); | |
}, | |
{ concurrency: map.concurrency } | |
); | |
// the reduce part | |
await invokeLambdaAsync<TReduce>(reduce.functionName, reduce.payload); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment