coify
Transform generator methods into co-wrapped functions.
Installing
npm install --save coify
Examples
Plain JavaScript objects
const coify = require('coify');
function delay( n ) {
return new Promise(function( resolve, reject ) {
setTimeout( resolve, n );
});
}
let obj = {
*asyncStuff() {
yield delay( 500 );
return true;
}
};
module.exports = coify( obj );
obj.asyncStuff().then( console.log );
Classes/Constructors
const coify = require('coify');
function delay( n ) {
return new Promise(function( resolve, reject ) {
setTimeout( resolve, n );
});
}
class Thing {
*asyncStuff() {
yield delay( 500 );
return true;
}
}
module.exports = coify( Thing );
const thing = new Thing();
thing.asyncStuff().then( console.log );