# consumable-stream

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

Latest version **3.0.0** (published 2023-09-06) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 23/100 (F)** — status: abandoned.

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2023-09-06 |
| First published | 2019-02-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/consumable-stream) |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Jonathan Gros-Dubois |
| Maintainers | topcloudsystems |
| Keywords | async, generator, stream, data, for-await-of |

## Links

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

## 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

- 3.0.0 (latest) — 2023-09-06
- 2.0.0 — 2020-02-07
- 1.0.0 — 2019-02-17

## README

# consumable-stream
A readable stream which can be iterated over using a for-await-of loop or using a while loop with await inside.

The `ConsumableStream` constructor is an abstract class which must be subclassed. Its `next()` and `createConsumer()` methods must be overriden.

For a concrete subclass of `ConsumableStream`, see `WritableConsumableStream`: https://github.com/SocketCluster/writable-consumable-stream

## Installation

```
npm install consumable-stream
```

## Usage

The `ConsumableStream` class exposes the following methods:

- `[Symbol.asyncIterator]`: Makes the instance iterable using a for-await-of loop.
- `next`: Returns a `Promise` which will resolve an object in the form `{value: data, done: boolean}` whenever some data is received or when the stream ends.
- `once`: Similar to `next()` except that the resolved value will be the raw data and it will not resolve when the stream ends. Note that once `once()` is called, it cannot be cancelled; the affected closure will stay in memory until either `once()` resolves or until the stream is ended or garbage collected.

```js
// Consume data objects from consumableStream as they are written to the stream.
(async () => {
  for await (let data of consumableStream) {
    console.log(data);
  }
})();

// Consume only the next data object which is written to the stream.
(async () => {
  let data = await consumableStream.once();
  console.log(data);
})();
```

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