Created
December 14, 2021 14:49
-
-
Save fersilva16/bdf2d0b9a5eedbcf7232760df876a87d to your computer and use it in GitHub Desktop.
Async iterators utils
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 asyncEach<T>( | |
array: { map(callback: (value: T) => any): Promise<any>[] }, | |
iteratee: (value: T) => Promise<any>, | |
) { | |
return Promise.all(array.map(iteratee)); | |
} | |
export async function asyncEachSeries<T>( | |
array: { | |
reduce(callback: (prev: any, value: T, index: number) => any, initialValue: any): any; | |
}, | |
iteratee: (value: T, index: number) => Promise<any>, | |
): Promise<void> { | |
return array.reduce( | |
(promise, value, index) => promise.then(() => iteratee(value, index)), | |
Promise.resolve(), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment