Created
January 11, 2022 00:25
-
-
Save fersilva16/e3ba600f4f8120822186b84990650dce to your computer and use it in GitHub Desktop.
Async array reduce implementation
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<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