promise-class
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 = ;var database = ...; ; PromiseClass;
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;
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
; name: 'joe';; // 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;
coroutines
any method that is a generator, and has the $deferred argument automatically becomes a coroutine
* { var one = this; $deferred; }
reopen
you can add methods to an instance by passing them via .reopen
like this
var user = ; user;
and you can add methods to a class like this
User;
when you should not reopen and override existing methods because you cant use super
mixins
var Mixin = { return Math; }; var Mixin2 = { $deferred; } * { this; }; ; // examplesvar example = ; example; example; example;
mixins should only use initialize to store instance vars
var Mixin = { this_tags = ; } { return this_tagslength; } { return this_tags !== -1; } { if this return; this_tags; };
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; ;
Or
var users = BlueBird;