trequire
thunkify an entire module at once. Useful if you want to use normal, asynchronous modules with co. All newly created, thunkified functions are prefixed with "co"
Installation
$ npm install trequire
Examples
You can thunkify entire modules simply by using trequire
in place of require
:
var trequire = ;var fs = ; fs; fs{ // all functions also have a thunkified version};
This works on modules like fs
which are just a collection of functions
as well as on modules that construct and return objects (so long as those
underlying objects can be accessed by recursively searching through the
module). For example, you can easily create a thunkified redis client using
trequire:
var trequire = ;var redis = ;var rcli = redis rcli; rcli{ // res = "OK"}; rcli; rcli{ // res = "bar"};
trequire
can also be called on objects. For example, to thunkify
only the RedisClient
instead of the entire module, you could do
the following:
var trequire = ;var redis = ; ; var rcli = redis; // rcli now has coget, coset, etc.
co
Use with Thunks make it easy to write code that works well with the
co
module. This allows you to write code that is still
asynchronous but in some cases is easier to read. co
currently requires you to run your code with the --harmony
flag as it utilizes javascript generators.
var trequire = ;var redis = ;var co = ; var rcli = redis;
For reference, this same code is repeated below in a standard asynchronous style:
var redis = ; var rcli = redis; // set the key "foo" to "bar"rcli
By thunkifying existing modules and using co
, you can leverage
existing node modules while easily avoiding the common node pitfall
of callback hell spaghetti code.
License
MIT