npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

trequire

v0.0.7

Published

Thunkify an entire module at once.

Downloads

6

Readme

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 = require('trequire');
var fs = trequire('fs');

fs.readFile('package.json', 'utf8', function(err, str){
  // normal async versions of all functions are still available
});

fs.coreadFile('package.json', 'utf8')(function(err, str){
  // 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 = require('trequire');
var redis = trequire('redis');
var rcli = redis.createClient()

rcli.set('foo', 'bar', function(err, res){
  // res = "OK"
});

rcli.coset('foo', 'bar')(function(err, res){
  // res = "OK"
});

rcli.get('foo', function(err, res){
  // res = "bar"
});

rcli.coget('foo')(function(err, res){
  // 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 = require('trequire');
var redis = require('redis');

trequire(redis.RedisClient);

var rcli = redis.createClient();

// rcli now has coget, coset, etc.

Use with co

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 = require('trequire');
var redis = trequire('redis');
var co = require('co');

var rcli = redis.createClient();

co(function *(){
    
    // set the key "foo" to "bar"
    yield rcli.coset("foo","bar");

    // retrieve the value of the key "foo"
    var res = yield rcli.coget("foo");
    // res = "bar"

})()

For reference, this same code is repeated below in a standard asynchronous style:


var redis = require('redis');

var rcli = redis.createClient();

// set the key "foo" to "bar"
rcli.set("foo", "bar", function(err){

    // retrieve the value of the key "foo"
    rcli.get("foo", function(err, res){

        // res = "bar"

    })

})

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