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 🙏

© 2025 – Pkg Stats / Ryan Hefner

promise-class

v0.0.6

Published

promises for classes made easy

Readme

promise-class Build Status

easy promises, coroutines, and a few other goodies!

  • by having a $deferred argument in a method you now have a function that returns a promise.
  • by having a $deferred argument in a generator method its now a coroutine

promise-class is the new promise-object

coroutines in PromiseClass are 15x faster than PromiseObject ;)

requirements

you must be using at minimum node 5.0.0

usage

here is an example class that looks up information on a user

var PromiseClass = require('promise-class');
var database = ...;

PromiseClass(class User {
	static *find ($deferred, id) {
		$deferred.resolve({
        	details: yield this.getDetails(id),
            friends: yield this.getFriends(id)
        });
    }
    
    static *getDetails($deferred, id) {
    	$deferred.resolve(yield database.query(...));
    }
    
    static *getFriends($deferred, id) {
    	$deferred.resolve(yield database.query(...));
    }
});

PromiseClass.wrap(function *() {
	var user = yield User.find(123);
})();

installation

npm install promise-class
node --harmony script.js

native promises?

by default PromiseClass will use BlueBird for promises if its in your node_modules directory.

if you would like to use another Promise library you can do the following

PromiseClass.setPromise(Q);

pseudo params

there are a few rules with these params

  • if you want to use $deferred it MUST be the first param
  • any other pseudo param must be before any real params

these pseudo params are supported

  • $deferred converts the method into a deferred method
  • $class returns the class
  • $self alternative to var self = this;
  • $*config, $*settings, $*options ensures that the first argument is an object

$*config / $*settings / $*options

helper that makes working with $config objects a little easier

PromiseClass(class User {
	constructor ($config) {
		this._name = $config.name;
    }
});

new User({name: 'joe'});
new User(); // this does not error out because the argument was replaced with an empty object

this would allow you to call the class method via Class.method

$deferred / promises

promoise-object is promise library agnostic, you initialize the wrapper by passing in the promise library you are using.

below is an example of using promises and showing errors

PromiseClass(class User {
	constructor (name) {
    	this._name = name;
    }
    
    getInfo ($deferred, error) {
		setTimeout(function () {
			if (error) {
				$deferred.reject(new Error('Something went wrong'));
			} else {
				$deferred.resolve({age: 12});
			}
		}, 1000);
	}
});

PromiseClass.wrap(function * {
	var joe = new User('joe');
    
	yield joe.getInfo(false);
    
    try {
    	joe.getInfo(true);
    } catch (error) {
    	console.log(error);
    }
})();

coroutines

any method that is a generator, and has the $deferred argument automatically becomes a coroutine

  *getInfo ($deferred) {
      var one = yield this.getSomething();
      $deferred.resolve(one);
  }

reopen

you can add methods to an instance by passing them via .reopen like this

var user = new User();

user.reopen({
	*getName ($deferred, $self) {
		setTimeout(function () {
			$deferred.resolve($self._name);
		}, 1000);
	}
});

and you can add methods to a class like this

User.reopen({
	*getName ($deferred, $self) {
		setTimeout(function () {
			$deferred.resolve($self._name);
		}, 1000);
	}
});

when you should not reopen and override existing methods because you cant use super

mixins

var Mixin =  {
	getRandomNumber () {
		return Math.random();
	}
};

var Mixin2 = {
	getRandomNumberDeferred ($deferred) {
		$deferred.resolve(Math.random());
	},
    
    *getRandomNumberCoroutine ($deferred) {
    	yield this.getRandomNumberDeferred();
	}
};

PromiseClass(class Class {
}, Mixin, Mixin2);

// examples
var example = new Class();

example.getRandomNumber();

example.getRandomNumberDeferred().then(function (number) {
	console.log(number);
});

example.getRandomNumberCoroutine().then(function (number) {
	console.log(number);
});

mixins should only use initialize to store instance vars

var Mixin =  {
	initialize () {
		this._tags = [];
	},
    
    get length () {
    	return this._tags.length;
    },

	hasTag (tag) {
		return this._tags.indexOf(tag) !== -1;
	},

	addTag (tag) {
		if (this.hasTag(tag)) return;

		this._tags.push(tag);
	}
};

anonymous classes

by default PromiseClasses are global in your file (not app), you can change this by doing the following

var User = PromiseClass.anonymous(class User {
});

or

var User = PromiseClass.anonymous(class {
});

wrap

Convience method for creating a coroutine

var getUser = PromiseClass.wrap(function *(id) {
	var user = yield getUser(id);
	...
	return user;
});

getUser(123).then(function (user) {
	console.log(user);
});

Or

var users = yield BlueBird.map(userIds, PromiseClass.wrap(function *(id) {
	var user = yield getUser(id);
	...
	return user;
}));