promise-object
provides a base object that gives you the ability to create promise methods just by setting the first parameter to $deferred and also binds those methods to the instance. It also allows you to extend any method and use mixins.
promise-object is very tiny - 1.3KB gzipped (3.1KB not gzipped)
installation
npm install promise-object
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
- $super returns the parent method
- $class returns the class
- $self alternative to var self = this;
- $config ensures that the first argument is an object
$config
helper that makes working with $config objects a little easier
var Promise = PromiseObject = Promise; var User = PromiseObject; name: 'joe';; // this does not error out because $config was replaced with an empty object
class / instance methods
you can specify class methods by placing a $ in front of the method like this
var Class = PromiseObject;
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
var Promise = PromiseObject = Promise; var User = PromiseObject; var joe = 'joe';joe;
deferred generators
if you are using a promise library that has coroutine
support (like bluebird) you can do the following
{ var one = this; $deferred;}
extending
any method can be extended upon, $super is used to request the parent method
var Promise = PromiseObject = Promise; var User = PromiseObject; var Admin = User; var joe = 'joe';joe;
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 reopen a method that already exists you gain access to $super
mixins
var Promise = PromiseObject = Promise; var Mixin = { return Math; }; var Mixin2 = { $deferred; }; var Class = PromiseObject; // examplesvar example = ; console; example;
mixins should only use initialize to store instance vars
var Mixin = { this_tags = ; } { return this_tags !== -1; } { if this return; this_tags; };