Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/littlstar/gean

"Composable" Generator Control
https://github.com/littlstar/gean

Last synced: about 2 months ago
JSON representation

"Composable" Generator Control

Awesome Lists containing this project

README

        

gean
====

gean provides a mechanism for composable generator control with
promises.

## why ?

* tiny.
* zero dependencies.
* composable.
* `Promise` based.

## installation

```sh
$ npm install gean --save
```

## usage

```js
gean(function * () {
// flow control here
});
```

`gean` accept a `GeneratorFunction` and returns a `Promise`
that allow for composability.

## example

We can use `gean` to create simple a `fetch` function backed by
[superagent](https://github.com/visionmedia/superagent). We'll use it to
read from the `randomuser` API and sum the number of `male` and `female`
gender entries per user.

```js
'use strict';
import agent from 'superagent';
import gean from 'gean';

const fetch = uri => new Promise((yep, nope) => {
agent.get(uri).end((err, res) => err ? nope(err) : yep(res));
});

gean(function * () {
const uri = 'https://randomuser.me/api?results=10';
const res = yield fetch(uri);
const results = res.body.results;
const counter = {female: 0, male: 0};

for (let result of results)
counter[result.user.gender]++;

console.log(counter)
// { female: 8, male: 2 }
});
```

## license

MIT