https://github.com/tomeraberbach/betterator
💯 A better sync and async iterator API.
https://github.com/tomeraberbach/betterator
async-iterable async-iterator iterable iterator javascript npm-package
Last synced: about 2 months ago
JSON representation
💯 A better sync and async iterator API.
- Host: GitHub
- URL: https://github.com/tomeraberbach/betterator
- Owner: TomerAberbach
- License: apache-2.0
- Created: 2021-03-22T03:42:27.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-10-15T03:47:20.000Z (7 months ago)
- Last Synced: 2025-03-31T11:12:24.071Z (about 2 months ago)
- Topics: async-iterable, async-iterator, iterable, iterator, javascript, npm-package
- Language: TypeScript
- Homepage: https://npm.im/betterator
- Size: 307 KB
- Stars: 58
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Contributing: contributing.md
- License: license
Awesome Lists containing this project
README
betterator
A better sync and async iterator API.## Features
- **Intuitive:** easy to use `hasNext` and `getNext` methods
- **Familiar:** lots of other programming languages use the same API
- **Tiny:** ~400 bytes minzipped
- **Awesome Name:** you have to admit it's pretty rad :sunglasses:## Install
```sh
$ npm i betterator
```## Usage
```js
import { Betterator, AsyncBetterator } from 'betterator'const slothActivities = [`sleeping`, `eating`, `climbing`]
// Or `new Betterator(slothActivities[Symbol.iterator]())`
const iterator = Betterator.fromIterable(slothActivities)while (iterator.hasNext()) {
console.log(iterator.getNext())
}
//=> sleeping
//=> eating
//=> climbingtry {
iterator.getNext()
} catch (e) {
console.log(e.message)
}
//=> Doesn't have nextconsole.log(iterator.getNextOr(() => `being lazy`))
//=> being lazyconst asyncSlothActivities = (async function* () {
yield* slothActivities
})()// Or `new AsyncBetterator(slothActivities[Symbol.asyncIterator]())`
const asyncIterator = AsyncBetterator.fromAsyncIterable(asyncSlothActivities)while (await asyncIterator.hasNext()) {
console.log(await asyncIterator.getNext())
}
//=> sleeping
//=> eating
//=> climbingtry {
await asyncIterator.getNext()
} catch (e) {
console.log(e.message)
}
//=> Doesn't have nextconst delay = timeout => new Promise(resolve => setTimeout(resolve, timeout))
console.log(
await asyncIterator.getNextOr(() => delay(10).then(() => `being lazy`)),
)
//=> being lazy
```See the [type definitions](https://unpkg.com/betterator/dist/index.d.ts) for
more documentation.## Contributing
Stars are always welcome!
For bugs and feature requests,
[please create an issue](https://github.com/TomerAberbach/betterator/issues/new).For pull requests, please read the
[contributing guidelines](https://github.com/TomerAberbach/betterator/blob/main/contributing.md).## License
[Apache 2.0](https://github.com/TomerAberbach/betterator/blob/main/license)
This is not an official Google product.