prelude

@prelude/async-generator · v2.0.1

SourceAPI referenceChangelognpm

Async generator module

A comprehensive utility library for working with asynchronous generators in TypeScript. This package provides a rich set of composable functions that simplify complex asynchronous data processing workflows, following functional programming principles.

This library empowers developers to efficiently handle streaming data, implement backpressure, process items concurrently, and transform async data flows with minimal boilerplate. From basic operations like mapping, filtering, and reducing to advanced patterns like batching, windowing, and controlled concurrency, @prelude/async-generator offers a consistent API that makes working with asynchronous sequences as intuitive as working with arrays. The fully typed implementation ensures type safety throughout your async processing pipelines.

Installation

npm i -E @prelude/async-generator

Usage

import * as G from '@prelude/async-generator'

API Reference

Cancellation

sleep, jitter, ofInterval, map, tap and consume accept signal in their options. Aborting makes the generator throw (or consume reject) with signal.reason, clears timers and stops pulling from the source, which is returned. map/tap throw at once — concurrent ones fail their output and close their input, serial ones race the pending pull and mapping against the signal — and drop the results of mappings still in flight; consume awaits callbacks already in flight before rejecting.

const controller = new AbortController()
await G.pipe(
  source,
  G.map(fetchOne, { concurrency: 4, signal: controller.signal }),
  G.consume(store, { signal: controller.signal })
)

License

This package is dedicated to the public domain under CC0 1.0.