Async Generator Pipeline Compose (JavaScript)
Compose async generators into a pipeline for lazy streaming transformation with backpressure.
Code
export async function* pipe(source,...transforms){let current=source;for(const transform of transforms){current=transform(current);}yield* current;}export function map(fn){return async function*(iter){for await(const item of iter)yield fn(item);};}export function filter(pred){return async function*(iter){for await(const item of iter)if(pred(item))yield item;};}export function take(n){return async function*(iter){let i=0;for await(const item of iter){if(i++>=n)return;yield item;}};}export function batch(size){return async function*(iter){let buf=[];for await(const item of iter){buf.push(item);if(buf.length>=size){yield buf;buf=[];}}if(buf.length>0)yield buf;};}Report a problem with this exampleOpens a pre-filled issue on GitHub.