Skip to content

Instantly share code, notes, and snippets.

@vivmaha
Created January 28, 2022 17:43
Show Gist options
  • Save vivmaha/14f90d30f53fc27efa2f386147388b0a to your computer and use it in GitHub Desktop.
Save vivmaha/14f90d30f53fc27efa2f386147388b0a to your computer and use it in GitHub Desktop.
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