# data-async-iterators

> Batteries-included utility functions to work with async iterables available in ES2018/TypeScript

Latest version **1.4.7** (published 2026-01-09) · ISC license · 0 weekly downloads

## Install

```sh
npm install data-async-iterators
pnpm add data-async-iterators
yarn add data-async-iterators
bun add data-async-iterators
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 1.4.7 |
| Published | 2026-01-09 |
| First published | 2018-06-01 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 745.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Pedro M. Silva |
| Maintainers | pedromsilva |
| Keywords | js, future, deferred, async, promises, iterables, iterators, generators, loop, streams, pull |

## Links

- npm: https://www.npmjs.com/package/data-async-iterators
- Repository: https://github.com/pedromsilvapt/data-async-iterator
- Homepage: https://github.com/pedromsilvapt/data-async-iterator#readme
- Issues: https://github.com/pedromsilvapt/data-async-iterator/issues
- npm.io page: https://npm.io/package/data-async-iterators

## Dependencies (7)

- [data-optional](https://npm.io/package/data-optional.md) 0.0.2
- [eventemitter3](https://npm.io/package/eventemitter3.md) ^3.0.1
- [data-semaphore](https://npm.io/package/data-semaphore.md) ^0.3.7
- [data-collectors](https://npm.io/package/data-collectors.md) ^1.0.1
- [data-cancel-token](https://npm.io/package/data-cancel-token.md) ^0.5.2
- [@pedromsilva/data-either](https://npm.io/package/@pedromsilva/data-either.md) 0.0.2
- [@pedromsilva/data-future](https://npm.io/package/@pedromsilva/data-future.md) ^1.0.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.4.7 (latest) — 2026-01-09
- 1.4.6 — 2022-04-24
- 1.4.4 — 2019-04-24
- 1.4.3 — 2019-04-23
- 1.4.2 — 2019-03-13
- 1.4.1 — 2019-03-13
- 1.4.0 — 2019-01-29
- 1.3.8 — 2019-01-28
- 1.3.7 — 2019-01-26
- 1.3.6 — 2019-01-25
- 1.3.5 — 2019-01-25
- 1.3.4 — 2018-12-19
- 1.3.3 — 2018-12-12
- 1.3.1 — 2018-12-07
- 1.3.0 — 2018-12-04
- … 15 more at https://npm.io/package/data-async-iterators/versions

## README

# Async Iterator

> Batteries-included utility functions to work with async iterables as available in ES2018/TypeScript

# Installation
```shell
npm install --save data-async-iterators
```

# Tips & Tricks
 - Iterators as lazy/pull-based
    - They only calculate the next value when it is requested; thus only calculating the values that are needed
 - Some methods require buffering values: be careful when mixing them with slow consumers
 - Iterators need consumers: since transformations are lazy, not consuming (subscribing) to an iterator means nothing happens
 - When manually using an iterator (calling `next()`), one should be careful to call `return()` on iterators that provide it as well, when the iterator is not needed anymore before it has ended, to allow it to free any resources it might be holding
 - Most operators return iterabtles. If provided with iterables as well, they can be iterated multiple times (instead of just once). Other iterators return iterators: these can only be iterated once
 - Most operators in this library accept `AsyncIterableLike<T>` instead of `AsyncIterable<T>`. This means certain rules apply:
    - `Iterable<T>`'s are transformed to `AsyncIterable<T>`'s;
    - `Iterator<T>`'s and `AsyncIetrator<T>`'s are transformed to `AsyncIterable<T>`'s that always return the same, original iterator;
    - `Promise<AsyncIterableLike<T>>`'s are converted to `AsyncIterable<T>`, waiting for the promise before using the resolved iterable;
    - The operator `fromPromise<T>( promise : Promise<T> )` returns an `AsyncIterable<T>` that only ever emits one value or one exception, whatever is resolved by the promise;

# Usage
Contains all the common utility functions like map, filter, takeWhile, flatMap, concat, and many more as well as more async-centric ones
like flatMapConcurrent, debounce, throttle, buffered, etc...

```typescript
import { from, delay, map, flatMapConcurrent } from 'data-async-iterators';

// Create an asynchonous iterable stream
const source = delay( from( [ 1, 2, 3, 4 ] ), 1000 );

// A closure that takes a number and slowly returns the number and it's square
const mapper = number => delay( from( [ number, number * number ] ), 4000 );

// Run mapper concurrently only twice
const flatMapConcurrent( source, mapper, 2 );

// And finally consume the values (returns a promise notifying when the iterator ends)
forEach( source, res => console.log( res ) );
```

Or maybe a more pratical example
```typescript
import { merge, map, forEach } from 'data-async-iterators';

function findDevices () : AsyncIterable<Device> { /* ... */ };

function connectDevice ( device : Device ) : AsyncIterable<DeviceStatus> { /* ... */ };

function processStatus ( status : DeviceStatus ) : Promise<void> { /* ... */ };

// Gets an async iterable of devices found
const devices : AsyncIterable<Device> = findDevices();

// For each iterable calls the connectDevice that returns an iterable documenting the statuses changes of each device
const statuses : AsyncIterable<DeviceStatus> = merge( map( devices, connectDevice ) );

// Consumes all 
forEach( statuses, processStatus );
```

Sometimes chaining functions in this way is not very readable, and therefore this package provides a utility class called `AsyncStream` that is a simple wraper around an iterable with all the operators as methods.

```typescript
import { AsyncStream } from 'data-async-iterators';

const stream = AsyncStream.range( 1, 10 )
    // Delay each number by 100 milliseconds
    .delay( 100 )
    // Double each number
    .map( v => v * 2 )
    // For each n number, generate n repetitions
    .flatMap( v => AsyncStream.repeat( v, v ) )
    // Ignore the first and last ten numbers
    .slice( 10, -10 );

// Since AsyncStream is a regular iterable, we can
for await ( let number of stream ) {
    console.log( number );
}

// Or
stream.forEach( number => console.log( number ) );

// To convert any regular AsyncIterable (or promises, regular iterables, arrays, etc...)
// into an AsyncStream just do:
const stream = new AsyncStream( iterable );
```

---
_Source: https://npm.io/package/data-async-iterators · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
