An open API service indexing awesome lists of open source software.

https://github.com/parro-it/asynciterable

Basic async iterable class with buffer.
https://github.com/parro-it/asynciterable

Last synced: 10 months ago
JSON representation

Basic async iterable class with buffer.

Awesome Lists containing this project

README

          

# asynciterable

[![Travis Build Status](https://img.shields.io/travis/parro-it/asynciterable/master.svg)](http://travis-ci.org/parro-it/asynciterable)
[![NPM downloads](https://img.shields.io/npm/dt/asynciterable.svg)](https://npmjs.org/package/asynciterable)

> Async iterable class

AsyncIterable is a class that implement the `async iterable` JavaScript pattern,
using a semantic similar to the promise one.

## Usage

Create an async iterable that emit three numbers:

```js
import AsyncIterable from 'asynciterable';

const numbers = new AsyncIterable((write, end, error) => {
write(1);
write(2);
write(3);
end();
});

for await (const n of numbers) {
console.log(n)
}

// Output: 1\n2\n3\n
```

## API

### AsyncIterable

A class that implement the `async iterable` JavaScript pattern, using a semantic
similar to the promise one.

**Parameters**

* `executor`
**[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)**
A function that is passed with the arguments write, end, error. The executor
function is executed immediately by the AsyncIterable implementation, passing
write, end and error functions (the executor is called before the
AsyncIterable constructor even returns the created object). The write
function, when called, make the async iterable emit a new item. The end
function, when called, make the async iterable end. The error function make it
throw an error. The executor normally initiates some asynchronous work, and
then, once a new item is available, it emit it by calling the write function,
or else call the error function if an error occurred. If an error is thrown in
the executor function, any subsequent call to iterator next is rejected with
the same error. The return value of the executor is ignored, unless if it is a
promise. In this case, when it fullfills to a rejection, error function is
automatically called.

## Install

With [npm](https://npmjs.org/) installed, run

```bash
npm install --save asynciterable
```

## See Also

* [`noffle/common-readme`](https://github.com/noffle/common-readme)

## License

MIT