# writable-consumable-stream

> An async stream which can be iterated over using a for-await-of loop.

Latest version **4.2.0** (published 2025-05-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install writable-consumable-stream
pnpm add writable-consumable-stream
yarn add writable-consumable-stream
bun add writable-consumable-stream
```

## Health

**Score 33/100 (F)** — status: maintenance-mode.

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

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.2.0 |
| Published | 2025-05-17 |
| First published | 2019-02-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/writable-consumable-stream) |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 61.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Jonathan Gros-Dubois |
| Maintainers | topcloudsystems |
| Keywords | async, generator, stream, data, for-await-of |

## Links

- npm: https://www.npmjs.com/package/writable-consumable-stream
- Repository: https://github.com/SocketCluster/writable-consumable-stream
- Homepage: https://github.com/SocketCluster/writable-consumable-stream#readme
- Issues: https://github.com/SocketCluster/writable-consumable-stream/issues
- npm.io page: https://npm.io/package/writable-consumable-stream

## Dependencies (1)

- [consumable-stream](https://npm.io/package/consumable-stream.md) ^3.0.0

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 4.2.0 (latest) — 2025-05-17
- 4.1.0 — 2023-10-20
- 4.0.1 — 2023-09-07
- 4.0.0 — 2023-09-06
- 3.0.1 — 2022-10-10
- 3.0.0 — 2022-10-10
- 2.0.0 — 2020-02-07
- 1.1.1 — 2019-02-20
- 1.1.0 — 2019-02-20
- 1.0.3 — 2019-02-18
- 1.0.2 — 2019-02-18
- 1.0.1 — 2019-02-17
- 1.0.0 — 2019-02-17

## README

# writable-consumable-stream
An async stream which can be iterated over using a for-await-of loop and which can be written to.

The `WritableConsumableStream` class extends the `ConsumableStream` class.  
See https://github.com/SocketCluster/consumable-stream

## Installation

```
npm install writable-consumable-stream
```

## Usage

### Require

```js
const WritableConsumableStream = require('writable-consumable-stream');
```

### Consume a stream and write to it asynchronously:

```js
let consumableStream = new WritableConsumableStream();

async function consumeAsyncIterable(asyncIterable) {
  // Consume iterable data asynchronously.
  for await (let packet of asyncIterable) {
    console.log('Packet:', packet);
  }
}
consumeAsyncIterable(consumableStream);

setInterval(() => {
  // Write data to the stream asynchronously,
  consumableStream.write(`Timestamp: ${Date.now()}`);
}, 100);
```

### Consume a stream using a while loop:

```js
let consumableStream = new WritableConsumableStream();

async function consumeAsyncIterable(asyncIterable) {
  // Consume iterable data asynchronously.
  // Works in older environments.
  let asyncIterator = asyncIterable.createConsumer();
  while (true) {
    let packet = await asyncIterator.next();
    if (packet.done) break;
    console.log('Packet:', packet.value);
  }
}
consumeAsyncIterable(consumableStream);

setInterval(() => {
  // Write data to the stream asynchronously,
  consumableStream.write(`Timestamp: ${Date.now()}`);
}, 100);
```

### Consume a filtered stream using an async generator:

```js
let consumableStream = new WritableConsumableStream();

// Creates an async generator which only produces packets which are allowed by the
// specified filterFunction.
async function* createFilteredStreamGenerator(fullStream, filterFunction) {
  for await (let packet of fullStream) {
    if (filterFunction(packet)) {
      yield packet;
    }
  }
}

async function consumeAsyncIterable(asyncIterable) {
  // Consume iterable data asynchronously.
  for await (let packet of asyncIterable) {
    console.log('Packet:', packet);
  }
}

// The filter function will only include strings which end with the number 5.
function filterFn(data) {
  return /5$/.test(data);
}
let filteredStreamGenerator = createFilteredStreamGenerator(consumableStream, filterFn);

consumeAsyncIterable(filteredStreamGenerator);

setInterval(() => {
  // Write data to the stream asynchronously,
  consumableStream.write(`Timestamp: ${Date.now()}`);
}, 100);
```

### Consume only the next data object which will be written to the stream:

```js
let consumableStream = new WritableConsumableStream();

(async () => {
  let data = await consumableStream.once();
  console.log(data);
})();

setInterval(() => {
  // Write data to the stream asynchronously,
  consumableStream.write(`Timestamp: ${Date.now()}`);
}, 100);
```

See `test/` directory for additional examples.

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