Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/wschella/asynciteratorasync

An AsyncIterator extensions that allows for async closures in utility methods
https://github.com/wschella/asynciteratorasync

Last synced: 6 days ago
JSON representation

An AsyncIterator extensions that allows for async closures in utility methods

Awesome Lists containing this project

README

        

# AsyncFilterIterator

An AsyncIterator extensions that allows for filtering with Promise based filters.

Use it like this:

```ts
const oldStream: AsyncIterator = whateverAsyncIteratorYouHadBefore;
const filter: (item: T) => Promise = someFilterYouWantToApply;
const newStream = new AsyncFilterIterator(filter, oldStream);
```

The only actual code is this:

```ts
import { AsyncIterator, TransformIterator } from 'asynciterator';

export class AsyncFilterIterator extends TransformIterator {
constructor(public _filter: (item: T) => Promise, source: AsyncIterator) {
super({ source });
}

_transform(item: T, done: any) {
this._filter(item)
.then((result) => (result) ? this._push(item) : undefined)
.then(() => done())
.catch((error) => {
this.emit('error', error);
done();
});
};
}
```