0.0.6 • Published 8 years ago
gean v0.0.6
gean
gean provides a mechanism for composable generator control with promises.
why ?
- tiny.
- zero dependencies.
- composable.
Promise
based.
installation
$ npm install gean --save
usage
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. We'll use it to
read from the randomuser
API and sum the number of male
and female
gender entries per user.
import agent from 'superagent';
import gean from 'gean';
const fetch = uri => new Promise((yep, nope) => {
agent.get(uri).end((err, res) ? nope(err) : yep(res));
};
gean(function * () {
const uri = 'https://randomuser.me/api?results=10';
const res = yield fetch(uri);
const users = res.body.results;
const counter = {female: 0, male: 0};
for (let user of users)
counter[user.gender]++;
console.log(counter)
// { female: 8, male: 2 }
});
license
MIT