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.
- Host: GitHub
- URL: https://github.com/parro-it/asynciterable
- Owner: parro-it
- License: mit
- Created: 2017-11-17T13:53:47.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-04T19:57:42.000Z (about 8 years ago)
- Last Synced: 2024-10-12T23:42:14.818Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 286 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: license
Awesome Lists containing this project
README
# asynciterable
[](http://travis-ci.org/parro-it/asynciterable)
[](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