# break-async-iterator

> Break async iterator

Latest version **0.1.0** (published 2018-08-21) · 0 weekly downloads

## Install

```sh
npm install break-async-iterator
pnpm add break-async-iterator
yarn add break-async-iterator
bun add break-async-iterator
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2018-08-21 |
| First published | 2018-08-21 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | laggingreflex |

## Links

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

## Recent versions

- 0.1.0 (latest) — 2018-08-21

## README

# break-async-iterator

**Problem:**

<!-- https://github.com/tc39/proposal-async-iteration/issues/126#issuecomment-403454433 -->

```js
const neverResolves /*(without user input)*/ = process.stdin /* (Readable is an async iterator as of Node v10) */
async function* stuck() {
  for await (const i of neverResolves) {
    yield // never reaches
  }
}
let i = stuck()
i.next() // stuck
i.return() // still stuck
```

**Solution**

```js
const breakAI = require('break-async-iterator')

const notStuck = breakAI(breakable => async function*() {
  try {
    for await (const i of breakable(neverResolves)) {
      yield
    }
  } finally {
    // reaches when `i.return()` or `i.throw()` is called
  }
})
let i = notStuck()
i.next() // still stuck
i.return() // but at least this can break the for-await loop
```

## Install

```
npm i break-async-iterator
```

## Usage

### API

**`breakAI(breakable => async function* asyncGenerator(){...})`**

* **`breakable`** A function that takes an iterator or a promise and returns a breakable version of the same

* **Returns** An async generator that returns an iterator (wrapped around the one returned by `asyncGenerator()`) that's able to interrupt any iterators or promises (that were passed through **`breakable()`**) when its `.return()` or `.throw()` are called

### Example

If you have an async generator function like this:

```js
async function* asyncGenerator() {
  for await (const value of anotherAsyncIterator) {
    yield value
  }
  yield await aPromise
}
```

Then simply wrap it like this

```js
const asyncGenerator = breakAI(breakable => async function*() {
  // and wrap any inner asyncIterators or promises with breakable:
  for await (const value of breakable(anotherAsyncIterator)) {
    yield value
  }
  yield await breakable(aPromise)
})
```

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