Turn each node function in an object return a thunk. Turn a regular node function into one which returns a thunk, useful for generator-based flow control such as co.
Installation
Example
// the same as thunkifyvar thunkify = ;var fs = ; fsreadFile = ; fs{ }; // thunkfiy an objectvar user = {} {} {} moduleexports = ;// module.exports = thunkify(user, ['add', 'show']);// module.exports = thunkify(user, 'add');
genify
Wrap every function return a GeneratorFunction
,
that will be easy to write codes in only one way: yield* fn()
.
var genify = genify;var fs = ; fsreadFile = ; var content = fs;
event support
you can pass an event object, give end event name list, wrap event to thunk like this
var e = new EventEmitter();
var end = thunkify.event(e, 'finish');
yield end();
or
yield.end(['close', 'end']); // will cover `finish` event
when specified events emitted, this generator will go on. see more in the source code.
ctx
also you can pass ctx
as contenxt into thunkify, and thunkify(object)
will use object as the context by default.
var thunkify = ;var { thisa = a; thisb = b;}; Calprototype { var self = this; ;}; Calprototype { var self = this; ;}; moduleexports = Cal; exports { return ;}; // orexports { var cal = a b; calplus = ; calminus = ;};
methods
by pass methods
list, support only thunkify a part of methods in an object.
exports.create3 = function (a, b) {
var cal = new Cal(a, b);
thunkify(cal, cal, ['plus']);
// or
thunkify(cal, ['plus']);
};
License
MIT