# @async-generators/from-emitter

> convert an event-emitter to an async-iterable

Latest version **0.3.0** (published 2018-01-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install @async-generators/from-emitter
pnpm add @async-generators/from-emitter
yarn add @async-generators/from-emitter
bun add @async-generators/from-emitter
```

## Health

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

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

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2018-01-16 |
| First published | 2017-10-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=9.0.0 |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Meirion Hughes |
| Maintainers | meirionhughes |
| Keywords | async, iterator, generator, esnext, typescript, linq, events |

## Links

- npm: https://www.npmjs.com/package/@async-generators/from-emitter
- Repository: https://github.com/async-generators/from-emitter
- Homepage: https://github.com/async-generators/from-emitter#readme
- Issues: https://github.com/async-generators/from-emitter/issues
- npm.io page: https://npm.io/package/@async-generators/from-emitter

## Dependencies (2)

- [@async-generators/subject](https://npm.io/package/@async-generators/subject.md) ^0.4
- [@async-generators/terminator](https://npm.io/package/@async-generators/terminator.md) ^0.4

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.3.0 (latest) — 2018-01-16
- 0.2.0 — 2018-01-12
- 0.1.1 — 2017-10-01
- 0.1.0 — 2017-10-01

## README

# from-emitter
[![logo](https://avatars1.githubusercontent.com/u/31987273?v=4&s=110)][async-url]

inform an iterable when it is prematurely terminated by the consumer. 

[![NPM version][npm-image]][npm-url]
[![Travis Status][travis-image]][travis-url]
[![Travis Status][codecov-image]][codecov-url]

### Install
```
yarn add @async-generators/from-emitter
```

This package's `main` entry points to a `commonjs` dist. 
The `module` entry points to a `es2015` module dist. Both require native async-generator support, or be down-compiled with a webpack loader. 


## Exports

### (default) from-emitter(emitter, onNext, onError, onDone [,selectNext][,selectError])

<code>from-emitter()</code> subscribes to `onNext`, `onError` and `onDone` and returns a (one-time) iterable-sequence of captured events. When the event listeners are called, the arguments are passed to `selectNext(...args)` and `selectError(...args)` to pick a value (defaults to the first argument). If the sequence completes (`onDone`), or the consumer terminates early, the event listeners are detached from the emitter and the iterable becomes disposed and cannot be iterated again.  

source must be a node-js compliment event emitter, with the `addListener ` and `removeListener` methods. 

## Example

example.js
```js
const fromEmitter = require('./dist/commonjs').default;
const {EventEmitter} = require('events');

async function main() {
  let events = new EventEmitter();
  let source = fromEmitter(events, "data", "error", "close");
  let consumer = new Promise(async done => {
    for await (let item of source) {
      console.log(item);
    }
    console.log("...and we're done!")
    done();
  });

  events.emit("data", 1);
  events.emit("data", 2);
  events.emit("data", 3);
  events.emit("data", 4);
  events.emit("close");

  await consumer;
}

main().catch(console.log);
```

Execute with the latest node.js: 

```
node example.js // or...
node --harmony-async-iteration example.js 
```

output:
```
1
2
3
4
...and we're done!
```
## Typescript

This library is fully typed and can be imported using: 

```ts
import fromEmitter from '@async-generators/from-emitter');
```

It is also possible to directly execute your [properly configured](https://stackoverflow.com/a/43694282/1657476) typescript with [ts-node](https://www.npmjs.com/package/ts-node):

```
ts-node --harmony_async_iteration example.ts
```

[npm-url]: https://npmjs.org/package/@async-generators/from-emitter
[npm-image]: https://img.shields.io/npm/v/@async-generators/from-emitter.svg
[npm-downloads]: https://img.shields.io/npm/dm/@async-generators/from-emitter.svg
[travis-url]: https://travis-ci.org/async-generators/from-emitter
[travis-image]: https://img.shields.io/travis/async-generators/from-emitter/master.svg
[codecov-url]: https://codecov.io/gh/async-generators/from-emitter
[codecov-image]: https://codecov.io/gh/async-generators/from-emitter/branch/master/graph/badge.svg
[async-url]: https://github.com/async-generators

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