Created
December 13, 2021 12:56
-
-
Save ExcitedSpider/ede7827ca75c717a51d7bf8e9ca03339 to your computer and use it in GitHub Desktop.
array reduce in async way
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
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