coroutinify
v0.0.3
Published
generator based control flow middlware
Downloads
2
Maintainers
Readme
Coroutinify
Coroutinify is a simple Generator based control flow middlware package for Node.js.
It's like Bluebirds' coroutine and uses it internally. In spite of Bluebird's coroutine, it can be used not only with GeneratorFunctions but also with objects which contain GeneratorFunctions. It supports ES6 classes and Symbol keys too.
In case of object the original functions are not removed but added Gen suffix to its name.
Usage
For single function
const co = require('coroutinify');
function* generator() {
let index = 0;
while(index < 3)
yield index++;
return index;
}
co(generator)()
.then(value => console.log(value));
For the whole object which also has Symbol keys
const co = require('coroutinify');
const Promise = require('bluebird');
let objcet = {};
object.gen1 = function* () {
let index = 0;
while(index < 3)
yield index++;
return index;
};
objcet.gen2 = function* () {
let index = 0;
while(index < 3)
yield index++;
return index;
};
objcet[Symbol.for('gen3')] = function* () {
let index = 0;
while(index < 3)
yield index++;
return index;
};
co(object);
Promise.all([
object.gen1(),
object.gen2(),
objcet[Symbol.for('gen3')]()])
.then(results => console.log(results));
For ES6 class instances
const co = require('coroutinify');
const Promise = require('bluebird');
class ES6 {
*gen1 () { yield 1; }
*gen2 () { yield 2; }
*[Symbol.for('gen3')] { yield 3; }
}
let es6Instance = new ES6();
co(es6Instance);
Promise.all([
es6Instance.gen1(),
es6Instance.gen2(),
es6Instance[Symbol.for('gen3')]()])
.then(results => console.log(results));