Skip to content

Instantly share code, notes, and snippets.

@n1ru4l
Created September 21, 2020 16:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save n1ru4l/127178705cc0942cad0e45d425e2eb63 to your computer and use it in GitHub Desktop.
Save n1ru4l/127178705cc0942cad0e45d425e2eb63 to your computer and use it in GitHub Desktop.
AsyncIteratorUtilities.ts
export const map = <T, O>(map: (input: T) => Promise<O> | O) =>
async function* mapGenerator(asyncIterable: AsyncIterableIterator<T>) {
for await (const value of asyncIterable) {
yield map(value);
}
};
export const filter = <T, U extends T>(filter: (input: T) => input is U) =>
async function* filterGenerator(asyncIterable: AsyncIterableIterator<T>) {
for await (const value of asyncIterable) {
if (filter(value)) {
yield value;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment