Skip to content

Instantly share code, notes, and snippets.

@fersilva16
Created January 11, 2022 00:25
Show Gist options
  • Save fersilva16/e3ba600f4f8120822186b84990650dce to your computer and use it in GitHub Desktop.
Save fersilva16/e3ba600f4f8120822186b84990650dce to your computer and use it in GitHub Desktop.
Async array reduce implementation
export async function asyncReduce<T, U>(
array: T[],
iteratee: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => Promise<U>,
initialValue: U
): Promise<U> {
return array.reduce(
(promise, currentValue, currentIndex) =>
promise.then((previousValue) => iteratee(previousValue, currentValue, currentIndex, array)),
Promise.resolve(initialValue)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment