Created
September 21, 2020 16:05
-
-
Save n1ru4l/127178705cc0942cad0e45d425e2eb63 to your computer and use it in GitHub Desktop.
AsyncIteratorUtilities.ts
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 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