Skip to content

Instantly share code, notes, and snippets.

@fersilva16
Created December 14, 2021 14:49
Show Gist options
  • Save fersilva16/bdf2d0b9a5eedbcf7232760df876a87d to your computer and use it in GitHub Desktop.
Save fersilva16/bdf2d0b9a5eedbcf7232760df876a87d to your computer and use it in GitHub Desktop.
Async iterators utils
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