Skip to content

Instantly share code, notes, and snippets.

@maraisr
Forked from n1ru4l/AsyncIteratorUtilities.ts
Created November 24, 2020 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maraisr/c91b874fb75b78ddfb03e5fd403e9ebb to your computer and use it in GitHub Desktop.
Save maraisr/c91b874fb75b78ddfb03e5fd403e9ebb 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