Skip to content

Instantly share code, notes, and snippets.

@ExcitedSpider
Created December 13, 2021 12:56
Show Gist options
  • Save ExcitedSpider/ede7827ca75c717a51d7bf8e9ca03339 to your computer and use it in GitHub Desktop.
Save ExcitedSpider/ede7827ca75c717a51d7bf8e9ca03339 to your computer and use it in GitHub Desktop.
array reduce in async way
export async function asyncReduce<IterationItem, AccValue>(
array: IterationItem[],
reducer: (accumulate: AccValue, current: IterationItem) => AccValue | Promise<AccValue>,
initValue: AccValue,
): Promise<AccValue> {
let accumulate = typeof initValue === 'object' ? Object.create((initValue as unknown) as object) : initValue;
for (const item of array) {
accumulate = await reducer(accumulate, item);
}
return accumulate;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment